switch statement

Q2: Use a switch statement with breaks between the cases. Create a program that asks the
user for two float numbers. Then asks the user if they would like to:
1. add the numbers
2. subtract the numbers
3. multiply the numbers
4. divide the numbers

The program should then print the numbers with the chosen operation and the solution.
For example:
The user enters: 4 5 +
The program will output: 4 + 5 = 9

#include <iostream>
int main()
{
float int1, int2;
cout<<"enter tow numbers;\n";
cin>>int1;
cin>>int2;
cout<<"enter the operation '+','-','*','/':\n";
return 0;
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
using namespace std;
int main()
{
float int1, int2;
char ch;
cout << "Enter two numbers : ";
cin >> int1 >> int2;
cout << "Enter a operator (+,-,*,/) : ";
cin >> ch;
switch (ch)
{
                case '+' :
			cout << int1 << " + " << int2 << " = " << int1+int2;
			break;
		case '-' :
			cout << int1 << " - " << int2 << " = " << int1-int2;
			break;
		case '*' :
			cout << int1 << " * " << int2 << " = " << int1*int2;
			break;
		case '/' :
			cout << int1 << " / " << int2 << " = " << int1/int2;
			break;
		default :
			cout << "\nError! You have entered a wrong operator. Please  try again.";
			break;
}
return 0;
}
Last edited on
Switch statement is used to switch between values . Each switch is terminated by a break statement. Switch is of the type char or int only.
Topic archived. No new replies allowed.