C Program to Make a Simple Calculator
C program to make a simple calculator that performs the addition, subtraction, multiplication, and division operations.
Example-1 Addition
Enter an operator from these - (+, -, *, /): +
Enter the two values to make calculation
90
35
Result: 125.00
Example-2 Subtraction
Enter an operator from these - (+, -, *, /): -
Enter the two values to make calculation
90
35
Result: 55.00
Example-3 Multiplication
Enter an operator from these - (+, -, *, /): *
Enter the two values to make calculation
10
45
Result: 450.00
Example-4 Division
Enter an operator from these - (+, -, *, /): /
Enter the two values to make calculation
90
25
Result: 3.60
There are different ways to implement this program like:
- Using the if else
- Using the switch statement
- Using the functions
C program to make a simple calculator using if else
// C program to make a simple calculator using if else
#include <stdio.h>
#include <limits.h>
int main()
{
char operator;
double operand1 = 0, operand2 = 0;
double result = 0;
// INPUT the operator
printf("Enter an operator from these - (+, -, *, /): ");
scanf("%c", &operator);
// INPUT the two values
printf("\nEnter the two values to make calculation\n");
scanf("%lf %lf", &operand1, &operand2);
if (operator== '+')
{
// Addition operation
result = operand1 + operand2;
}
else if (operator== '-')
{
// Subtraction operation
result = operand1 - operand2;
}
else if (operator== '*')
{
// Multiplication operation
result = operand1 * operand2;
}
else if (operator== '/')
{
// Division operation
// Check for division by zero
if (operand2 != 0)
{
result = operand1 / operand2;
}
else
{
printf("\nError! Division by zero.\n");
result = INT_MIN;
}
}
else
{
printf("\nError! Operator is not correct.\n");
result = INT_MIN;
}
printf("\nResult: %.2lf\n", result);
return 0;
}
Output
Enter an operator from these - (+, -, *, /): +
Enter the two values to make calculation
90
35
Result: 125.00
C program to make a simple calculator using switch statement
// C program to make a simple calculator using switch statement
#include <stdio.h>
#include <limits.h>
int main()
{
char operator;
double operand1 = 0, operand2 = 0;
double result = 0;
// INPUT the operator
printf("Enter an operator from these - (+, -, *, /): ");
scanf("%c", &operator);
// INPUT the two values
printf("\nEnter the two values to make calculation\n");
scanf("%lf %lf", &operand1, &operand2);
switch (operator)
{
// Addition operation
case '+':
result = operand1 + operand2;
break;
// Subtraction operation
case '-':
result = operand1 - operand2;
break;
// Multiplication operation
case '*':
result = operand1 * operand2;
break;
// Division operation
case '/':
// Check for division by zero
if (operand2 != 0)
{
result = operand1 / operand2;
}
else
{
printf("\nError! Division by zero.\n");
result = INT_MIN;
}
break;
default:
printf("\nError! Operator is not correct.\n");
result = INT_MIN;
break;
}
printf("\nResult: %.2lf\n", result);
return 0;
}
Output
Enter an operator from these - (+, -, *, /): -
Enter the two values to make calculation
90
35
Result: 55.00
C program to make a simple calculator using functions
// C program to make a simple calculator using functions
#include <stdio.h>
#include <limits.h>
// Addition operation
double addition(double operand1, double operand2)
{
return operand1 + operand2;
}
// Subtraction operation
double subtraction(double operand1, double operand2)
{
return operand1 - operand2;
}
// Multiplication operation
double multiplication(double operand1, double operand2)
{
return operand1 * operand2;
}
// Division operation
double division(double operand1, double operand2)
{
// Check for division by zero
if (operand2 != 0)
{
return operand1 / operand2;
}
else
{
printf("\nError! Division by zero.\n");
return INT_MIN;
}
}
int main()
{
char operator;
double operand1 = 0, operand2 = 0;
double result = 0;
// INPUT the operator
printf("Enter an operator from these - (+, -, *, /): ");
scanf("%c", &operator);
// INPUT the two values
printf("\nEnter the two values to make calculation\n");
scanf("%lf %lf", &operand1, &operand2);
if (operator== '+')
{
result = addition(operand1, operand2);
}
else if (operator== '-')
{
result = subtraction(operand1, operand2);
}
else if (operator== '*')
{
result = multiplication(operand1, operand2);
}
else if (operator== '/')
{
result = division(operand1, operand2);
}
else
{
printf("\nError! Operator is not correct.\n");
result = INT_MIN;
}
printf("\nResult: %.2lf\n", result);
return 0;
}
Output
Enter an operator from these - (+, -, *, /): *
Enter the two values to make calculation
25
60
Result: 1500.00