A simple calculator performs basic arithmetic operations such as addition, subtraction, multiplication, and division on two numbers.
- The operation is selected using an arithmetic operator.
- The calculator can be implemented using switch or if-else statements.
Examples
Input: a = 10, b = 5, op = +
Output: 15.00
Explanation: Chosen operation is addition, so 10 + 5 = 15Input: a = 12, b = 3, op = /
Output: 4.00
Explanation: The chosen operation is division, so 12 / 3 = 4
Methods to Create a Simple Calculator
A simple calculator can be implemented using conditional statements to select and perform the required arithmetic operation.
Using switch Statement
The switch statement provides a clean way to select an operation based on the input operator. Each case handles one arithmetic operation.
#include <stdio.h>
#include <float.h>
int main() {
char op;
double a, b, res;
// Read the operator
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &op);
// Read the two numbers
printf("Enter two operands: ");
scanf("%lf %lf", &a, &b);
// Define all four operations in the corresponding
// switch-case
switch (op) {
case '+':
res = a + b;
break;
case '-':
res = a - b;
break;
case '*':
res = a * b;
break;
case '/':
res = a / b;
break;
default:
printf("Error! Incorrect Operator Value\n");
res = -DBL_MAX;
}
if(res!=-DBL_MAX)
printf("%.2lf", res);
return 0;
}
Input
Enter an operator (+, -, *, /): +
Enter two operands: 10 5
Output
15.00Explanation
- The program reads the operator and two operands using scanf().
- The switch statement performs the corresponding arithmetic operation and stores the result in res.
- break exits the matched case, while the default case handles an invalid operator.
- DBL_MAX is used as a marker to avoid printing a result when the operator is invalid.
Note: The break statement in each switch case is used to come out of the switch statement when same case is matched and executed. Otherwise, all the cases after the matching case will be executed.
Using if-else Statement
The if-else if ladder is an alternative but more complex way to create a C program for a simple calculator. The idea is to use a series of if-else-if statements to check the vale of the input operator, perform the corresponding operation. and return the result.
#include <float.h>
#include <stdio.h>
int main() {
char op;
double a, b, res;
// Read the operator
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &op);
// Read the two numbers
printf("Enter two operands: ");
scanf("%lf %lf", &a, &b);
// Perform the operation corresponding to the
// given operator
if (op == '+')
res = a + b;
else if (op == '-')
res = a - b;
else if (op == '*')
res = a * b;
else if (op == '/')
res = a / b;
else {
printf("Error! Operator is not correct.\n");
res = -DBL_MAX;
}
if (res != -DBL_MAX)
printf("Result: %.2lf\n", res);
return 0;
}
Input
Enter an operator (+, -, *, /): +
Enter two operands: 10 5
Output
15.00Explanation
- The program reads the operator and two operands using scanf().
- The if-else if ladder checks the operator and performs the corresponding arithmetic operation.
- If the operator is invalid, the default else block displays an error message.
- DBL_MAX is used as a marker to prevent printing a result when the operator is invalid.