Calculator Program in C
Algorithm to implement calculator program:
- Declare the following four variables: n1, n2, res, and operator. The variables n1 and n2 are used to store the values or operands; res is used to store the calculation result; and operator is used to take the operator as input.
- Operator, take input from the operands.
- To test the operator, use switch-case or conditional statements.
- Display the outcome based on the operator.
- Close the programme.
We can implement calculator program in different ways. The following are some ways to implement.
- The switch statement is used in a C calculator programme.
- Calculator programme written in C with if-else-if statements.
- Calculator programme written in C with a do-while loop and a switch statement.
Implementation of Calculator Program Using Switch Statement:
#include <stdio.h>
int main()
{
// Declare variables for local use
char operator;
int n1, n2;
float res;
printf ("Select any operator from the following: \n+, -, *, or / \nIn order to perform the calculations in calculator \n ");
scanf ("%c", &operator);
///Display the selected operator.
if (operator == '/' )
{
printf (" The operator selected is Division");
}
else if (operator == '*')
{
printf (" The operator selected is Multiplication");
}
else if (operator == '-')
{
printf (" The operator selected is Subtraction");
}
else if (operator == '+')
{
printf (" The operator selected is Addition");
}
//First Number can be taken
printf (" \n Please Enter the first Number: ");
scanf(" %d", &n1);
//Second Number can be taken
printf ("\n Please Enter the second number: ");
scanf (" %d", &n2);
switch(operator)
{
case '+':
res = n1 + n2;
printf (" Sum of %d and %d is: %.2f", n1, n2, res);
break;
case '-':
res = n1 - n2;
printf (" Difference of %d and %d is: %.2f", n1, n2, res);
break;
case '*':
res = n1 * n2;
printf (" Product of %d and %d is: %.2f", n1, n2, res);
break;
case '/':
if (n2 == 0)
{
printf (" \n It is not possible to divide by zero. Please insert a different number. ");
scanf ("%d", &n2);
}
res = n1 / n2;
printf (" divide of %d and %d is: %.2f", n1, n2, res);
break;
default:
printf (" The operators must be only the following: +, -, *, / ");
}
return 0;
}
OUTPUT:
Implementation of Calculator Program Using if-else-if Statement:
#include <stdio.h>
int main()
{
// Declare variables for local use
char operator;
int n1, n2;
float res;
printf (" Select any operator from the following: \n+, -, *, or / \nIn order to perform the calculations in calculator \n ");
scanf ("%c", &operator);
printf (" Please enter the first number: ");
scanf(" %d", &n1);
printf (" Please enter the second number: ");
scanf (" %d", &n2);
if (operator == '+')
{
res = n1 + n2; // sum of the numbers
printf (" Sum of %d and %d is: %f", n1, n2, res);
}
else if (operator == '-')
{
res = n1 - n2; // difference of the numbers
printf (" Differnce of %d and %d is: %f", n1, n2, res);
}
else if (operator == '*')
{
res = n1 * n2; // Product of the numbers
printf ("Product of %d and %d is: %f", n1, n2, res);
}
else if (operator == '/')
{
if (n2 == 0)
{
printf (" \n It is not possible to divide by zero. Please insert a different number.");
scanf ("%d", &n2);
}
res = n1 / n2; // division of numbers
printf (" divide of %d and %d is: %.2f", n1, n2, res);
}
else
{
printf(" \n Wrong operator inserted. The operators must be only the following: +, -, *, / ");
}
return 0;
}
OUTPUT:
Implementation of Calculator Program Using do-while Statement:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main()
{
int operator, n1, n2;
float res;
char c;
do
{
printf (" Select any operator from the following: \n+, -, *, or / \nIn order to perform the calculations in calculator");
printf (" \n 1 Add\n 2 Sub\n 3 Mul\n 4 Div \n 5 Exit\n ");
scanf ("%d", &operator);
switch (operator)
{
case 1:
// Sum of the numbers
printf (" The operator selected is Addition");
printf ("\n Please enter the first number: ");
scanf (" %d", &n1);
printf (" \n Please enter the second number: ");
scanf (" %d", &n2);
res = n1 + n2;
printf (" Sum of %d and %d is: %.2f",n1,n2, res);
break;
case 2:
// difference of numbers
printf ("The operator selected is subtraction ");
printf ("\n Please enter the first number: ");
scanf (" %d", &n1);
printf (" \n Please enter second number: ");
scanf (" %d", &n2);
res = n1 - n2;
printf ("Product of %d and %d is: %.2f",n1,n2, res);
break;
case 3:
// Product of numbers
printf (" The operator selected is Multiplication");
printf ("\n Please Enter the first number: ");
scanf (" %d", &n1);
printf (" \nPlease enter second number: ");
scanf (" %d", &n2);
res = n1 * n2;
printf ("Product of %d and %d is: %.2f", n1,n2,res);
break;
case 4:
// Division
printf (" The operator selected is division");
printf ("\n Please enter the first number: ");
scanf (" %d", &n1);
printf (" \n Please enter the second number: ");
scanf (" %d", &n2);
if (n2 == 0)
{
printf (" \n It is not possible to divide by zero. Please insert a different number.");
scanf ("%d", &n2);
}
res = n1 / n2;
printf ("Divide of %d and %d is: %.2f",n1,n2, res);
break;
case 5:
printf (" Exit");
exit(0);
break;
default:
printf(" The operators must be only the following: +, -, *, / ");
break;
}
} while (operator != 4);
return 0;
}
OUPUT: