MVC ASP.NET C# SQL Server WCF Written Test HR Round Subscribe C# Videos C# Programs Buy DVD
Build a simple calculator using c# programming language. The calculator should have the following features.
1. Adding 2 Numbers
2. Subtracting 2 Numbers
3. Multiplying 2 Numbers
4. Dividing 2 Numbers

Also, the program should run as long as the user wants it to be running.

using System;
namespace SamplePrograms
{
    class SimpleCalculator
    {
        public static void Main()
        {
            string UserSelection = string.Empty;
            do
            {
                PrintMenu();
                int UserChoice = Convert.ToInt32(Console.ReadLine());
                while (UserChoice < 1 || UserChoice > 4)
                {
                    Console.WriteLine("Invalid Option. Try again");
                    PrintMenu();
                    Console.WriteLine();
                    UserChoice = Convert.ToInt32(Console.ReadLine());
                }
                Calculate(UserChoice);


                Console.WriteLine("Do you want to continue");
                UserSelection = Console.ReadLine();
                while (UserSelection.ToUpper() != "NO" &&
                       UserSelection.ToUpper() != "YES")
                {
                    Console.WriteLine();
                    Console.WriteLine("Invalid Input - Please type YES or NO");
                    Console.WriteLine("Do you want to continue");
                    UserSelection = Console.ReadLine();
                }
            }
            while (UserSelection.ToUpper() != "NO");
            Console.WriteLine();
            Console.WriteLine("The program will now terminate");
        }


        public static void PrintMenu()
        {
            Console.WriteLine();
            Console.WriteLine("Select your option");
            Console.WriteLine("------------------");
            Console.WriteLine("1. Addition");
            Console.WriteLine("2. Subtraction");
            Console.WriteLine("3. Multiplication");
            Console.WriteLine("4. Division");
            Console.WriteLine();
        }


        public static void Calculate(int SelectedOption)
        {
            int Result = 0;
            Console.WriteLine("Enter your first number");
            int FirstNumber = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter your second number");
            int SecondNumber = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine();


            switch (SelectedOption)
            {
                case 1:
                    Result = FirstNumber + SecondNumber;
                    Console.WriteLine("{0} + {1} = {2}", FirstNumber, SecondNumber, Result);
                    break;
                case 2:
                    Result = FirstNumber - SecondNumber;
                    Console.WriteLine("{0} - {1} = {2}", FirstNumber, SecondNumber, Result);
                    break;
                case 3:
                    Result = FirstNumber * SecondNumber;
                    Console.WriteLine("{0} * {1} = {2}", FirstNumber, SecondNumber, Result);
                    break;
                case 4:
                    Result = FirstNumber / SecondNumber;
                    Console.WriteLine("{0} / {1} = {2}", FirstNumber, SecondNumber, Result);
                    break;
            }
        }
    }
}


1 comment:

  1. using System;


    namespace Learning
    {
    class Program
    {
    static void Main()
    {
    int a, b , Result;
    Console.WriteLine("Welcome To My Calculator [Two Numbers]");
    Console.WriteLine("Operations\n1-Sum\n2-Sub\n3-Multi\n4-Div");
    Console.WriteLine("Choose Your Operation");
    int x = int.Parse(Console.ReadLine());
    if (x == 1)
    {
    Console.Write("Enter First Number:\n");
    a = int.Parse(Console.ReadLine());
    Console.Write("Enter Secound Number:\n");
    b = int.Parse(Console.ReadLine());
    Result = a + b;
    Console.WriteLine("Result: {0}", Result);
    }

    else if (x == 2)
    {
    Console.Write("Enter First Number:\n");
    a = int.Parse(Console.ReadLine());
    Console.Write("Enter Secound Number:\n");
    b = int.Parse(Console.ReadLine());
    Result = a - b;
    Console.WriteLine("Result: {0}", Result);
    }
    else if (x == 3)
    {
    Console.Write("Enter First Number:\n");
    a = int.Parse(Console.ReadLine());
    Console.Write("Enter Secound Number:\n");
    b = int.Parse(Console.ReadLine());
    Result = a * b;
    Console.WriteLine("Result: {0}", Result);
    }
    else if (x == 4)
    {
    Console.Write("Enter First Number:\n");
    a = int.Parse(Console.ReadLine());
    Console.Write("Enter Secound Number:\n");
    b = int.Parse(Console.ReadLine());
    if (b == 0)
    {
    Console.WriteLine("Secound Number Cannot be =0");

    }
    else
    {
    Result = a / b;
    Console.WriteLine("Result: {0}", Result);
    }
    }
    else
    Console.WriteLine("NOT AN AVAILABLE OPTION");
    Console.WriteLine("Thank You");

    }

    }

    }

    ReplyDelete