How do I to?:
Write a C# program that simulates a simple calculator. Initially, the user chooses an operation:
Please choose an operation (+,-,*,/):+
Repeat that user input until he/she has entered one of the four operator symbols (+, , *, /). Then, ask the user needs to enter two double operands:
Please enter first operand: 12.34
Please enter second operand: 45.67
Repeat that user input until the user has entered two numbers.
Now use a “switch” statement to test for the four different operands:
Switch
(operator) {
case “+”:
result = addNumbers (first, second);
break;
...
}
Each operand triggers invoking a separate method (addNumber,
subtractNumber, multiplyNumber, divideNumber) and assigning the result to a doublevariable “result”. Finally, print out:
The result of
Mohammed MusekulaPosted Mar 28, 2015, 8:42 AM
Pankaj Kumar ChoudharyPosted Mar 24, 2015, 8:15 PM
static void Main(string[] args)
{
double num1;
double num2;
int status = 1;
string operand;
double answer;
while (status == 1)
{
Console.WriteLine("Please enter the first num: ");
num1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(" Enter Your Operation among + , /,*, - ");
operand = Console.ReadLine();
Console.WriteLine("Please enter the second num: ");
num2 = Convert.ToInt32(Console.ReadLine());
switch (operand)
{
case "+":
answer = num1 + num2;
status = 1;
break;
case "-":
answer = num1 - num2;
status = 1;
break;
case "*":
answer = num1 * num2;
status = 1;
break;
case "/":
answer = num1 / num2;
status = 1;
break;
default:
answer = 0;
status = 0;
break;
}
if(status==1)
Console.WriteLine(num1.ToString() + " " + operand + " " + num2.ToString() + " = " + answer.ToString());
}
Console.ReadLine();
}
Abhishek JaiswalPosted Mar 24, 2015, 3:27 PM
Try this
{
{
Console
num1 = Convert.ToInt32
Console
Console
num2 = Convert.ToInt32
{
}
Console
Console
}
}