Calculator program in c Using Switch Case
Learn C - C tutorial - c program to make a simple calculator using switch case - C examples - C programs
C Program to Make a Simple Calculator Using switch case
- The operator is stored in a character variable 'op'.
- This program first takes two integer operands and an arithmetic operator as input from user.
- For perform arithmetic operation it uses a switch case statement.
- If none of the input operators matches then it prints error message on screen.
Sample Code
# include <stdio.h>
int main()
{
char op='*';
double firstNumber=90,secondNumber=45;
printf("Enter an operator (+, -, *,):* ");
printf("\nEnter two operands: ");
switch(op)
{
case '+':
printf("%.1lf + %.1lf = %.1lf",firstNumber, secondNumber, firstNumber + secondNumber);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf",firstNumber, secondNumber, firstNumber * secondNumber);
break;
default:
printf("Error! operator is not correct");
}
return 0;
}
Output
Enter an operator (+, -, *,):+
Enter two operands: 90.0 + 45.0 = 135.0