Question #22809

//write a program that enter two number and the sign and the output is sum or
subst or multi or divide according to sign hint (use switch )
Input : 1 + 2
Output :3
Input : 3 * 3
Output : 9 use switch

Expert's answer

#include <conio.h>
#include <iostream>

using namespace std;

void main()
{
int a, b;
char sign;
int result;

cout << "Enter an expression: ";
cin >> a >> sign >> b;

switch (sign)
{
case '+':
result = a + b;
break;

case '*':
result = a * b;
break;

case '-':
result = a - b;
break;

case '/':
result = a / b;
break;

default:
cout << "Invalid input." << endl;
getch();
return;
}

cout << "Result: " << result << endl;
getch();
}
LATEST TUTORIALS
APPROVED BY CLIENTS