Blog

C# calculator

Aug 19, 2009 Blogs | C# Programming
This blog will show how to create a calculator using C# Console Application.

class Program

    {

        static void Main(string[] args)

        {

            int num1;

            int num2;

            string operand;

            float answer;

 

 

            Console.Write("Please enter the first integer: ");

            num1 = Convert.ToInt32(Console.ReadLine());

 

 

            Console.Write("Please enter an operand (+, -, /, *): ");

            operand = Console.ReadLine();

 

 

            Console.Write("Please enter the second integer: ");

            num2 = Convert.ToInt32(Console.ReadLine());

 

            switch (operand)

            {

                case "-":

                    answer = num1 - num2;

                    break;

                case "+":

                    answer = num1 + num2;

                    break;

                case "/":

                    answer = num1 / num2;

                    break;

                case "*":

                    answer = num1 * num2;

                    break;

                default:

                    answer = 0;

                    break;

            }

 

            Console.WriteLine(num1.ToString() + " " + operand + " " + num2.ToString() + " = " + answer.ToString());

 

            Console.ReadLine();

        }

    }

Niradhip Chakraborty

Niradhip Chakraborty is a software developer from India. He has experience in C#, .Net framework, ASP.NET, Windows Applications, ADO.NET, Xml, Web Services, Custom Controls, and e-commerce web si...

  • 104rank
  • 289K readers
COMMENT USING

PREMIUM SPONSORS

Hire Mobile & Web Developer on demand. 100% satisfaction. Try for 1 week or Money Back. Local and remote developers available all over USA.

Latest Blogs