Write C# program to create simple calculator using switch


Introduction

I have used Visual Studio 2012 for debugging purpose. But you can use any version of visul studio as per your availability.

techstudy.org

Kindle Paperwhite High Resolution Display Tablet | Rs. 9999

  1. using System;
  2.  
  3. class Program
  4. {
  5.  
  6. static void Main(string[] args)
  7. {
  8.  
  9. int num1;
  10. int num2;
  11. string operand;
  12. float answer;
  13.  
  14. Console.Write("Please enter the first integer: ");
  15. num1 = Convert.ToInt32(Console.ReadLine());
  16.  
  17. Console.Write("Please enter an operand (+, -, /, *): ");
  18. operand = Console.ReadLine();
  19.  
  20. Console.Write("Please enter the second integer: ");
  21. num2 = Convert.ToInt32(Console.ReadLine());
  22.  
  23. switch (operand)
  24. {
  25.  
  26. case "-":
  27. answer = num1 - num2;
  28. break;
  29.  
  30. case "+":
  31. answer = num1 + num2;
  32. break;
  33.  
  34. case "/":
  35. answer = num1 / num2;
  36. break;
  37.  
  38. case "*":
  39. answer = num1 * num2;
  40. break;
  41.  
  42. default:
  43. answer = 0;
  44. break;
  45. }
  46. Console.WriteLine(num1.ToString() + " " + operand + " " + num2.ToString() + " = " + answer.ToString());
  47. Console.ReadLine();
  48.  
  49. }
  50.  
  51. }

Result

C# language techstudy

     





Related Topics


Responses




Popular Posts

Facebook

Founded in 2016, Tech Study passionately delivers stylish and dynamic innovative information to programmer and technology lovers. http://www.techstudy.org/.