The following C# code is for a simple calculator. This app is capable of performing addition, subtraction, multiplication as well as division.
C# Calculator Example in Console App
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
class Program { static void Main(string[] args) { int num1; int num2; string operand; ConsoleKeyInfo status; float answer; while (true) { Console.Write("Please enter the first integer: "); num1 = Convert.ToInt32(Console.ReadLine()); Console.Write("Please enter the second integer: "); num2 = Convert.ToInt32(Console.ReadLine()); Console.Write("Please enter an operand (+, -, /, *): "); operand = 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.WriteLine("\n\n Do You Want To Break (Y/y)"); status = Console.ReadKey(); if(status.Key==ConsoleKey.Y) { break; } Console.Clear(); } } } |
Output:
You can find more similar examples of programming for this programming language in the site.
You may also like : Calculator in C# Code Project