Howdy readers, today you will learn how to write a C program to make a simple calculator using a switch…case.
This program takes two numbers and an arithmetic operator (+, -, *, /) as input from the user. Then it uses a switch…case statement to perform a particular operation between the two numbers based on the operator entered by the user.
This calculator program will perform only basic operations like addition, subtraction, multiplication and division.
If the operator entered by the user doesn’t match any of these operators (+, -, *, /), then this program will display an error message on the screen.
So, without any delay, let’s begin this tutorial.
C Program to Make a Simple Calculator Using switch…case
C Program
// C Program to Make a Simple Calculator Using switch...case #include <stdio.h> int main(){ float num1, num2, result; char op; // Taking Input printf("Enter the operator (+, -, *, /): "); scanf("%c", &op); printf("Enter the first number: "); scanf("%f", &num1); printf("Enter the second number: "); scanf("%f", &num2); // Perform Operation Using switch...case switch(op){ case '+': result = num1 + num2; printf("%.2f + %.2f = %.2f", num1, num2, result); break; case '-': result = num1 - num2; printf("%.2f - %.2f = %.2f", num1, num2, result); break; case '*': result = num1 * num2; printf("%.2f * %.2f = %.2f", num1, num2, result); break; case '/': result = num1 / num2; printf("%.2f / %.2f = %.2f", num1, num2, result); break; default: printf("Invalid Operator!!"); } return 0; }
Output 1
Enter the operator (+, -, *, /): + Enter the first number: 5 Enter the second number: 8 5.00 + 8.00 = 13.00
Output 2
Enter the operator (+, -, *, /): - Enter the first number: 7 Enter the second number: 5 7.00 - 5.00 = 2.00
Output 3
Enter the operator (+, -, *, /): * Enter the first number: 3 Enter the second number: 9 3.00 * 9.00 = 27.00
Output 4
Enter the operator (+, -, *, /): / Enter the first number: 117 Enter the second number: 9 117.00 / 9.00 = 13.00

Explanation
float num1, num2, result; char op;
We have declared three float data type operators and one character data type variable num1, num2, result and op respectively.
printf("Enter the operator (+, -, *, /): "); scanf("%c", &op); printf("Enter the first number: "); scanf("%f", &num1); printf("Enter the second number: "); scanf("%f", &num2);
Then, this program asks the user to enter an operator (+, -, *, /) and two operands. The operator gets stored in the ‘op‘ variable, and the two operands get stored in the ‘num1‘ and ‘num2‘ variables.
switch(op)
Now, the switch case checks the validity of the operator. If the operator entered by the user is valid, then it will jump to the corresponding case.
case '+': result = num1 + num2; printf("%.2f + %.2f = %.2f", num1, num2, result); break;
If the operator entered by the user is ‘+’, then the control of the program will jump to case ‘+’: and the associated block of code is executed.
The break statement stops the execution.
default: printf("Invalid Operator!!"); }
If the operator entered by the user does not match any case, then it will execute the default case.
Conclusion
Today you learned to create a simple calculator in C programming language using the switch…case statement.
If you have any queries related to the program, comment down your queries in the comment section.
Thanks for reading.
Happy Coding!!
Also Read:
- C Program to Reverse a Number
- C Program to Check Prime Number
- C Program to Calculate Simple Interest
- C Program to Check Armstrong Numbers
- C Program to Find Sum of Upper Triangle Matrix