Advertisement
AvengersAssemble

Standard Calculator Using Switch & While

Sep 4th, 2013
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.59 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApplication14
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             //calculator
  14.             string exit = ".";
  15.             while (exit != "exit" && exit != "close")
  16.             {
  17.                 Console.WriteLine("Enter two numbers:");
  18.                 double num1 = double.Parse(Console.ReadLine());
  19.                 double num2 = double.Parse(Console.ReadLine());
  20.                 Console.WriteLine("Please choose operator (*, /, +, -):");
  21.                 string op = Console.ReadLine();
  22.                 switch (op)
  23.                 {
  24.                     case ("*"):
  25.                         Console.WriteLine(num1 * num2);
  26.                         break;
  27.  
  28.                     case ("/"):
  29.                         Console.WriteLine(num1 / num2);
  30.                         break;
  31.  
  32.                     case ("+"):
  33.                         Console.WriteLine(num1 + num2);
  34.                         break;
  35.  
  36.                     case ("-"):
  37.                         Console.WriteLine(num1 - num2);
  38.                         break;
  39.  
  40.                     default:
  41.                         Console.WriteLine("Invalid operator!");
  42.                         break;
  43.                 }
  44.                 Console.WriteLine("Type 'exit' or 'close' in order to exit the program. If you would like to\nchoose again, press enter.");
  45.                 exit = Console.ReadLine();
  46.                 Console.WriteLine();
  47.             }
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement