Simple Calculator Using Switch
C++
Easy
2 views
Problem Description
Make calculator that can add, subtract, multiply or divide.
Real Life: Like calculator on your phone.
Step-by-Step Logic:
1. Take two numbers
2. Take operation choice (+, -, *, /)
3. Use switch to select operation
4. Perform selected operation
5. Print result
Official Solution
void control_q5_calculator() {
int num1 = 20;
int num2 = 5;
char operation = '*';
cout << "Numbers: " << num1 << " and " << num2 << endl;
cout << "Operation: " << operation << endl;
switch(operation) {
case '+':
cout << "Result: " << num1 + num2 << endl;
break;
case '-':
cout << "Result: " << num1 - num2 << endl;
break;
case '*':
cout << "Result: " << num1 * num2 << endl;
break;
case '/':
cout << "Result: " << num1 / num2 << endl;
break;
default:
cout << "Invalid operation" << endl;
}
}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!