belongs to collection: Switch case C# programs with an examples
Write C# program to create simple calculator using switch
I have used Visual Studio 2012 for debugging purpose. But you can use any version of visul studio as per your availability.
using System; 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(); } }
Result:
Please enter the first integer: 50
Please enter an operand (+, -, /, *): /
Please enter the second integer: 5
50 / 5 = 10
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
I have used Visual Studio 2012 for debugging purpose. But you can use any version of visul studio as per your availability.
Result:
Please enter the first integer: 50
Please enter an operand (+, -, /, *): /
Please enter the second integer: 5
50 / 5 = 10
need an explanation for this answer? contact us directly to get an explanation for this answer