C Program for Calculator Using Switch Case

Calculator using switch statement in c. simple calculator program in c programming language. 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 simple calculator program in c for beginners

#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

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. Using this method we can execute the program to calculator using switch statement in c.

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.