C++ Program to perform arithmetic operations using switch case


In this program, You will learn how to perform arithmetic operations using switch case in C++.


Enter your choice = 1

10 + 10 = 20

Example: How to perform arithmetic operations using a switch case in C++.

#include<iostream>
using namespace std;

int main() {

    int x, y, res;
    int ch;

    cout << "Enter 1 For Addition :";
    cout << "\nEnter 2 For Subtraction :";
    cout << "\nEnter 3 For Multiplication :";
    cout << "\nEnter 4 For Division :";
    cout << "\nEnter 5 For Modulus :"<<endl;
    cin >> ch;

    switch (ch) {
        case 1:
        {
            cout << "Enter Two Numbers :";
            cin >> x >> y;

            res = x + y;

            cout << "\nResult is :" << res;
            break;
        }
        case 2:
        {
            cout << "Enter Two Numbers :";
            cin >> x >> y;

            res = x - y;

            cout << "Result is :" << res;
            break;
        }
        case 3:
        {
            cout << "Enter Two Numbers :";
            cin >> x >> y;

            res = x * y;

            cout << "Result is :" << res;
            break;
        }
        case 4:
        {
            cout << "Enter Two Numbers :";
            cin >> x >> y;

            res = x / y;

            cout << "Result is :" << res;
            break;
        }
        case 5:
        {
            cout << "Enter Two Numbers :";
            cin >> x >> y;

            res = x % y;

            cout << "Result is :" << res;
            break;
        }
    }

    return 0;
}

Output:

Enter 1 For Addition :
Enter 2 For Subtraction :
Enter 3 For Multiplication :
Enter 4 For Division :
Enter 5 For Modulus :
2
Enter Two Numbers :10 2
Result is :8