Home » C Programs » Calculator Program in C Using Switch Case

Calculator Program in C Using Switch Case

This program will execute perform basic task like add, sub, mul and div. To run and execute the basic operation can be done in the calculator program in c using switch case for beginners

Switch Case in C

Long if statements that compare a variable to many integral values can be replaced with switch case statements. A multiway branch statement is the switch statement. It makes it simple to route execution to various portions of code based on the expression’s value. 

Return Statement in C

A return statement marks the end of a function’s execution and hands control back to the calling function. The calling function continues execution at the place where the call was made. A return statement can give the calling function a value.

Calculator Program in C Using Switch Case

//Calculator Program in C Using Switch Case 
#include<stdio.h> 
int main()
{ 
int a, b, total;
char op; 
printf("Enter the number");
scanf("%d%c%d", &a, &op, &b);
switch(op)
{
case '+':
total = a + b;
break;
case '-':
total = a - b;
break;
case '*':
total = a * b;
break;
case '/':
total = a / b;
break; 
}
printf("total = %d", total);
return 0; 
}

Output

Enter the number

56+44

total = 100