How to make a very simple calculator C#

[By JayDev64x] [Index]

Here is how to make a very simple 2 number calculator capable of adding, subtracting, multiplying, and dividing using the console's input.

using System; namespace Script { class Example { static void Main() { Console.WriteLine("What operation do you want to use?"); Console.WriteLine("add | subtract | multiply | divide"); // Saves the operation wanted into a string. string operation = Console.ReadLine(); // Here is where the operation is read. if (operation == "add") { // Writes the operation entered. Console.WriteLine("Enter the 2 numbers you want to " + operation + "."); // This converts the input from the console which is normally a string into a float so they can be added. float number1 = Convert.ToSingle(Console.ReadLine()); float number2 = Convert.ToSingle(Console.ReadLine()); // This makes a variable of the number that is actually added. float answer = number1 + number2; // Then finally the sum is written down. Console.WriteLine("Answer:" + answer); } // NOTE: Every operation past this is essentially the same but with a different operation used in the answer variable. else if (operation == "subtract") { Console.WriteLine("Enter the 2 numbers you want to " + operation + "."); float number1 = Convert.ToSingle(Console.ReadLine()); float number2 = Convert.ToSingle(Console.ReadLine()); float answer = number1 - number2; Console.WriteLine("Answer:" + answer); } else if (operation == "multiply") { Console.WriteLine("Enter the 2 numbers you want to " + operation + "."); float number1 = Convert.ToSingle(Console.ReadLine()); float number2 = Convert.ToSingle(Console.ReadLine()); float answer = number1 * number2; Console.WriteLine("Answer:" + answer); } else if (operation == "divide") { Console.WriteLine("Enter the 2 numbers you want to " + operation + "."); float number1 = Convert.ToSingle(Console.ReadLine()); float number2 = Convert.ToSingle(Console.ReadLine()); float answer = number1 / number2; Console.WriteLine("Answer:" + answer); } // This is to make sure the user is aware the operation they entered did not match any that is recognized. else { Console.WriteLine("This is not a valid operation."); } Console.WriteLine("Press enter to reset."); Console.ReadLine(); // Loop the program over again and clear the screen. Console.Clear(); Main(); } } }

Created [September 2, 2021]