C++ Program: Calculator to Add, Subtract, Multiply, and Divide two numbers using switch statement

By: Prof. Dr. Fazal Rehman Shamil | Last updated: July 13, 2024

Write a Simple program in C++ for Calculator to Add, Subtract, Multiply, and Divide two numbers.

Example Outputs:

Example Output #1

Enter operator (+, -, *, /): +
Enter two numbers: 5 3
5 + 3 = 8

Example Output #2

Enter operator (+, -, *, /): –
Enter two numbers: 10 4
10 – 4 = 6

Example Output #3

Enter operator (+, -, *, /): *
Enter two numbers: 7 6
7 * 6 = 42

Example Output #4

Enter operator (+, -, *, /): /
Enter two numbers: 8 2
8 / 2 = 4

Example Output #5

Enter operator (+, -, *, /): /
Enter two numbers: 5 0
Error! Division by zero.

Example Output #6

Enter operator (+, -, *, /): &
Enter two numbers: 4 2
Error! Operator is not correct.

 

Memory Consumption for each variable

Variable NameData TypeDescriptionMemory Consumption
opcharOperator1 byte
num1doubleOperand 18 bytes
num2doubleOperand 28 bytes