Switch Operations: The 4 basic mathematical operations are Addition, Subtraction, Multiplication and Divison. They are actually the core part in every mathematical problem.
Develop a program to perform all the mathematical operations in a menu driven format using a Switch case statement.
Input and Output format:
Refer sample input and output for formatting specifications.
Sample Input & Output 1 :
[All text in bold corresponds to input and the rest to output.]
Enter the first value :
12
Enter the second value :
12
Enter the choice from the menu
1.Addition
2.Subtraction
3.Multiplication
4.Division
1
The value after Addition is 24.
Sample Input & Output 2 :
Enter the first value :
12
Enter the second value :
12
Enter the choice from the menu
1.Addition
2.Subtraction
3.Multiplication
4.Division
2
The value after Subtraction is 0.
Sample Input & Output 3 :
Enter the first value :
12
Enter the second value :
12
Enter the choice from the menu
1.Addition
2.Subtraction
3.Multiplication
4.Division
3
The value after Multiplication is 144.
Sample Input & Output 4 :
Enter the first value :
12
Enter the second value :
12
Enter the choice from the menu
1.Addition
2.Subtraction
3.Multiplication
4.Division
4
The value after Division is 1.
Solution
#include <iostream>
using namespace std;
int main()
{
int a, b;
cout << "Enter the first value :" << endl;
cin >> a;
cout << "Enter the second value :" << endl;
cin >> b;
cout << "Enter the choice from the menu\n1.Addition\n2.Subtraction\n3.Multiplication\n4.Division\n";
int choice;
//sauravhathi
cin >> choice;
switch (choice)
{
case 1:
cout << "The value after Addition is " << a + b << "." << endl;
break;
case 2:
cout << "The value after Subtraction is " << a - b << "." << endl;
break;
case 3:
// sauravhathi
cout << "The value after Multiplication is " << a * b << "." << endl;
break;
case 4:
cout << "The value after Division is " << a / b << "." << endl;
break;
default:
cout << "Invalid choice" << endl;
break;
}
return 0;
}
Happy Learning – If you require any further information, feel free to contact me.