Calculator - Switch - C# Programming Exercise

In this C# programming exercise, you need to create a program that asks the user for two numbers and a mathematical operation to perform between them. Supported operations are: +, -, *, x (multiplication), and / (division). The program will calculate and display the result of the chosen operation, as shown in the following example:

Example: Enter the first number: 5 Enter the operation: + Enter the second number: 7 Result: 5+7=12

You must use the switch statement to evaluate the user's input for the operation. The use of if is not allowed. This exercise focuses on practicing structured decision-making using the switch construct in C# programming.

 Category

Basic Data Types

 Exercise

Calculator - Switch

 Objective

Write a C# program that asks the user for two numbers and an operation to perform on them (+,-,*,x,/) and displays the result of that operation, as in this example:

Enter the first number: 5
Enter the operation: +
Enter the second number: 7
5+7=12

Note: You MUST use 'switch', not 'if'.

 Write Your C# Exercise

using System;  // Import the System namespace to use basic classes like Console

class Program  // Define the main class of the program
{
    static void Main()  // The entry point of the program
    {
        double num1, num2, result;  // Declare variables for two numbers and the result
        string operation;  // Declare a variable to store the operation

        // Ask the user for the first number
        Console.Write("Enter the first number: ");
        num1 = Convert.ToDouble(Console.ReadLine());  // Read and convert the input to double

        // Ask the user for the operation
        Console.Write("Enter operation (+, -, *, x, /): ");
        operation = Console.ReadLine();  // Read the operation as a string

        // Ask the user for the second number
        Console.Write("Enter the second number: ");
        num2 = Convert.ToDouble(Console.ReadLine());  // Read and convert the second number to double

        // Perform the operation using "switch"
        switch (operation)  // Use switch to determine the operation
        {
            case "+":  // Check if the operation is addition
                result = num1 + num2;  // Perform addition
                Console.WriteLine($"{num1} + {num2} = {result}");  // Display the result
                break;

            case "-":  // Check if the operation is subtraction
                result = num1 - num2;  // Perform subtraction
                Console.WriteLine($"{num1} - {num2} = {result}");  // Display the result
                break;

            case "*":  // Check if the operation is multiplication
            case "x":  // Allow both '*' and 'x' for multiplication
                result = num1 * num2;  // Perform multiplication
                Console.WriteLine($"{num1} * {num2} = {result}");  // Display the result
                break;

            case "/":  // Check if the operation is division
                if (num2 != 0)  // Check if the divisor is not zero
                {
                    result = num1 / num2;  // Perform division
                    Console.WriteLine($"{num1} / {num2} = {result}");  // Display the result
                }
                else  // If the divisor is zero
                {
                    Console.WriteLine("Error: Cannot divide by zero.");  // Display an error message
                }
                break;

            default:  // If the operation is not recognized
                Console.WriteLine("Invalid operation. Please use +, -, *, x, or /.");  // Display an error message
                break;
        }
    }
}

 Share this C# exercise

 More C# Programming Exercises of Basic Data Types

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  Double

    In this C# programming exercise, you need to create a program that calculates the perimeter, area, and diagonal of a rectangle, given its widt...

  •  Calculate values of a function

    In this C# programming exercise, you need to create a program that calculates and displays certain values of the function y = x² - 2x + 1, using integer numbers for ...

  •  Display a function

    In this C# programming exercise, you must create a program to "draw" the graph of the function y = (x - 4)² for integer values of x ranging from -1 to 8. The result will be visuall...

  •  Float, speed units

    In this C# exercise, you are asked to write a program that prompts the user for two important pieces of information: the distance in meters and the time taken,...

  •  Sphere, float

    In this C# exercise, you are required to write a program that calculates the surface area and volume of a sphere, given its radius. The formulas provided are as follo...

  •  Vowel - switch

    In this C# exercise, you are asked to write a program that prompts the user for a symbol and, using the switch statement, determines whether the symbol entered is a ...