In Visual Studio in C#, you will create a simple calculator that performs addition, subtraction, multiplication, and division. Your program should request a numerical input from the user, followed by the operation to be performed, and the second number to complete the equation. The result should be displayed to the user after each equation is performed. For example, if the user performs 3+3, the program should display 6 as the result.
The program should continue running so the user can perform multiple operations. For example, if the user wants to add, subtract, and then multiply they should be able to do each of these without exiting the program. If you need a reference use the simple calculator on your computer. You do not need to include a function for clearing the result.
SOURCE CODE:
*Please follow the comments to better understand the code.
**Please look at the Screenshot below and use this code to copy-paste.
***The code in the below screenshot is neatly indented for better understanding.
using System;
public class Calculator
{
static public void Main ()
{
Console.WriteLine("Welcome to C#
Calculator..!");
while(true)
{
Console.Write("\nEnter number1:
");
int number1 =
Convert.ToInt32(Console.ReadLine());
Console.Write("Enter operator
(+,-,*,/) : ");
char op =
Console.ReadKey().KeyChar;
Console.Write("\nEnter number2:
");
int number2 =
Convert.ToInt32(Console.ReadLine());
switch (op)
{
case '+':
Console.WriteLine(number1+" + "+number2+" =
"+(number1+number2));
break;
case '-':
Console.WriteLine(number1+" - "+number2+" =
"+(number1-number2));
break;
case '*':
Console.WriteLine(number1+" * "+number2+" =
"+(number1*number2));
break;
case '/':
Console.WriteLine(number1+" / "+number2+" =
"+number1/number2);
break;
default:
Console.WriteLine("Not a valid operation..!");
break;
}
Console.Write("Do you want to continue? (Y/N): ");
char choice=Console.ReadKey().KeyChar;
if(choice=='N')
break;
}
}
}
=================
Get Answers For Free
Most questions answered within 1 hours.