Showing posts with label c sharp. Show all posts
Showing posts with label c sharp. Show all posts

Wednesday, October 9, 2013

P2C2: Write a program in C Sharp to input an abbreviation and then output its meaning. If the abbreviation is not in the table, a suitable message should be output.

Input Table

Abbreviation
Meaning
m
metre
kg
kilogram
s
second
a
ampere
k
kelvin

//Source code in C Sharp


using System;
namespace Application_Abbreviation
{
    class Program_Abbreviation
    {
        static void Main(string[] args)
        {
            string abb;

            Console.WriteLine("Enter an abbreviation: ");
            abb = Convert.ToString(Console.ReadLine());

            switch (abb)
            {
                case "m":
                    Console.WriteLine("metre ");
                    break;

                case "kg":
                    Console.WriteLine("kilogram ");
                    break;

                case "s":
                    Console.WriteLine("second ");
                    break;

                case "a":
                    Console.WriteLine("ampere ");
                    break;

                case "k":
                    Console.WriteLine("kelvin ");
                    break;

                default:
                    Console.WriteLine("\nInvalid abbreviation");
                    break;

            }
        }
    }
}

OUTPUT

Enter an abbreviation:
kg
kilogram

Sunday, September 29, 2013

P2C2: C Sharp Program - Sum of 10 consecutive numbers using while, do ... while and for loop

// Using while loop

using System;

public class Program_Sum
{
   public static void Main()
    {
        int n=0, sum=0;
     
        while (n < 10)
        {
            n = n + 1;
            sum = sum + n;
            Console.WriteLine("Number = {0} Sum = {1}", n, sum);
        }


        Console.WriteLine("\n\nSum of {0} numbers = {1}", n, sum);
     
        Console.WriteLine("\nPress any key to exit...");
        Console.ReadLine();

    }
}

OUTPUT:

Number = 1 Sum = 1
Number = 2 Sum = 3
Number = 3 Sum = 6
Number = 4 Sum = 10
Number = 5 Sum = 15
Number = 6 Sum = 21
Number = 7 Sum = 28
Number = 8 Sum = 36
Number = 9 Sum = 45
Number = 10 Sum = 55


Sum of 10 numbers = 55

Press any key to exit...


//Using do ... while loop


using System;

public class Program_Sum
{
   public static void Main()
    {
        int n=0, sum=0;

        do
        {
            n = n + 1;
            sum = sum + n;
            Console.WriteLine("Number = {0} Sum = {1}", n, sum);
        } while (n < 10);


        Console.WriteLine("\n\nSum of {0} numbers = {1}", n, sum);
     
        Console.WriteLine("\nPress any key to exit...");
        Console.ReadLine();

    }
}

OUTPUT:

Number = 1 Sum = 1
Number = 2 Sum = 3
Number = 3 Sum = 6
Number = 4 Sum = 10
Number = 5 Sum = 15
Number = 6 Sum = 21
Number = 7 Sum = 28
Number = 8 Sum = 36
Number = 9 Sum = 45
Number = 10 Sum = 55


Sum of 10 numbers = 55

Press any key to exit...


//Using for loop

using System;

public class Program_Sum
{
   public static void Main()
    {
        int sum=0;

        for(int n=1; n<=10; n++)
        {
            sum = sum + n;
            Console.WriteLine("N = {0} \t Sum = {1}", n, sum);
        }

                 
        Console.WriteLine("\nPress any key to exit...");
        Console.ReadLine();

    }
}

OUTPUT:

N = 1    Sum = 1
N = 2    Sum = 3
N = 3    Sum = 6
N = 4    Sum = 10
N = 5    Sum = 15
N = 6    Sum = 21
N = 7    Sum = 28
N = 8    Sum = 36
N = 9    Sum = 45
N = 10   Sum = 55

Press any key to exit...

P2C2: C Sharp Program - Multiplication of two numbers using function

//Multiplication of two numbers using function
using System;

public class MultiplicationProgram
{
   static long Multiply (int n1, int n2)
    {
        return (n1 * n2);
    }

    public static void Main()
    {
        int num1, num2;
        long mul=1;

        Console.WriteLine("Enter First Number:");
        num1=Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("Enter Second Number:");
        num2 = Convert.ToInt32(Console.ReadLine());

        mul = Multiply(num1, num2);
     
        Console.WriteLine("\nMultiplication = {0} * {1} = {2}",num1, num2, mul);
        Console.WriteLine("\nPress any key to exit...");
        Console.ReadLine();

    }
}

OUTPUT:

Enter First Number:
10
Enter Second Number:
20

Multiplication = 10 * 20 = 200

Press any key to exit...

P2C2: C Sharp Program - Factorial using Recursion and Iteration

//Source Code using Recursion
using System;

public class Recursion_Factorial
{
   static long Factorial(int num)
    {
        if (num <= 1)
            return 1;
        return num * Factorial(num - 1);
    }

    public static void Main()
    {
        int num;
        long fact=1;

        Console.WriteLine("Enter a Number");
        num=Convert.ToInt32(Console.ReadLine());

        fact = Factorial(num);
     
        Console.WriteLine("Number = {0} and Factorial = {1}",num,fact);
        Console.WriteLine("\nPress any key to exit...");
        Console.ReadLine();

    }
}

OUTPUT:

Enter a Number
6
Number = 6 and Factorial = 720

Press any key to exit...

-------------------------------------------------------------------------------------

//Using Iteration Method
using System;

public class Program_Factorial_Iteration
{
   public static void Main()
    {
        int num;
        long ans;

        Console.WriteLine("Enter any number: ");
        num = Convert.ToInt32(Console.ReadLine());

        ans = factorial(num);
        Console.WriteLine("Factorial of " + num + " = " + ans);
       
        Console.WriteLine("\n\nPress any key to exit...");
        Console.ReadLine();
    }

   static long factorial(int n)
   {
       long fact;
       if (n <= 1)
           return 1;
       else
       {
           fact = 1;
           
           for (int i = n; i > 0; i--)
           {
               fact = fact * i;
           }
           return fact;
       }      
   }
}

OUTPUT:

Enter any number:
5
Factorial of 5 = 120


Press any key to exit...