Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C# (console) program to create an interface that acts as a simple two nu

ID: 3852837 • Letter: W

Question

Write a C# (console) program to create an interface that acts as a simple two number calculator. Your program should continue doing calculations until it is closed or exited. [2 Marks] Instead of a GUI keypad, this calculator will use console input to accept the two numbers to be used in the calculation. This calculator needs to prompt the user for ONE selection of any of these simple math operators: plus, minus, multiply, or divide. Three (3) inputs will need to be prompted: one for the selected mathematical operation, one for the first input, and one for the second input. The output will be displayed when the mathematical operation is known and the two inputs (numbers) are entered. The output will not be editable by the user. After the calculation is executed, the application will display error messages (or other status message), and can be as you deem appropriate. [4 marks] This calculator will handle all errors using try/catch statements. The error message output should display at least 3 different types of error (or status) messages, depending on what the user has done incorrectly. Your program should be able to catch all possible errors the user may do, it should not crash. [6 marks]

Explanation / Answer

using System;

public class Test
{
   public static void Main()
   {
        int operation = 0;
        double operand1,operand2,result;
        result = 0;
      
   do
   {
        //choose from menu of operations
        Console.WriteLine("1.Plus");
        Console.WriteLine("2.Minus");
        Console.WriteLine("3.Multiply");
        Console.WriteLine("4.Divide");
        Console.WriteLine("0.Stop");
        Console.WriteLine("Choose one operation");
      
   try        //inputs in try catch block
    {
       operation = Convert.ToInt32(Console.ReadLine());
       Console.WriteLine("Enter first operand: ");
       double a = Convert.ToDouble(Console.ReadLine());

       Console.WriteLine("Enter second operand: ");
       double b = Convert.ToDouble(Console.ReadLine());

       switch(operation)
       {
           case 1: result = a + b;
                    break;
           case 2: result = a - b;
                    break;
           case 3: result = a * b;
                    break;
           case 4: if ( b==0)
                    Console.WriteLine("cannot divide by zero"); //divide by 0 exception
                    else
                    result = a / b;
                    break;
           case 0:
                    return;
           default : Console.WriteLine("Invalid operation");
                    break;
       }
    }
  
   catch (FormatException)// will check wrong inputs
   {
       Console.WriteLine("You didn't enter number for operation or operands.");
   }
   Console.WriteLine("Result = "+result);
   }
   while(operation != 0);
   }
}


Output:

1.Plus

2.Minus

3.Multiply

4. Divide

0. Stop

Choose one operation 4

Enter first operand : 40

Enter second operand : 0

cannot divide by zero

result = 0

1.Plus

2.Minus

3.Multiply

4. Divide

0. Stop

Choose one operation 1

Enter first operand : 23

Enter second operand : 78

result = 101

1.Plus

2.Minus

3.Multiply

4. Divide

0. Stop

Choose one operation 0

Enter first operand :

Enter second operand :

cannot divide by zero

result = 0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote