Q-35: Write a menu driven C/C++ program using switch-case statement that acts as a basic calculator.
    

Using - C


#include<stdio.h> void main() { char ch; int num1, num2; input: printf("Enter Expression: "); scanf("%d %c %d", &num1, &ch, &num2); switch(ch) { case '+': printf("%d + %d = %d",num1, num2, (num1 + num2)); break; case '-': printf("%d - %d = %d",num1, num2, (num1 - num2)); break; case '*': printf("%d X %d = %d",num1, num2, (num1 * num2)); break; case '/': printf("%d / %d = %.2f",num1, num2, (num1 / (float)num2)); break; default: system("cls"); printf("Enter a valid expression! \n\n"); goto input; } }

OUTPUT

Enter Expression:  5*6
5 X 6 = 30

        

Using - C++


#include<iostream> using namespace std; int main() { char ch; int num1, num2; input: cout<< "Enter Expression: "; cin>> num1 >> ch >> num2; switch(ch) { case '+': cout<< num1 << " + " << num2 << " = " << (num1 + num2) << endl; break; case '-': cout<< num1 << " - " << num2 << " = " << (num1 - num2) << endl; break; case '*': cout<< num1 << " X " << num2 << " = " << (num1 * num2) << endl; break; case '/': cout<< num1 << " / " << num2 << " = " << (num1 / (float)num2) << endl; break; default: system("cls"); cout<< "Enter a valid expression! \n"<< endl; goto input; } return 0; }

OUTPUT

Enter Expression:  5/2
5 / 2 = 2.5