Home >>c programs >Write a C Program to design a calculator using switch case

Write a C Program to design a calculator using switch case

C Program to design calculator with basic operations using switch

In this example, we will see a C program to design a calculator with basic operations.

This program will read two integer numbers and an arithmetic operator like +,-,*,/,% and then print the result according to given operator using switch statement.

Example:

/*C program to design calculator with basic operations using switch.*/
#include <stdio.h>
int main()
{
int num1,num2;
float result;
char ch;    //to store operator choice
printf("Enter first number: ");
scanf("%d",&num1);
printf("Enter second number: ");
scanf("%d",&num2);
printf("Choose operation to perform (+,-,*,/): ");
scanf(" %c",&ch);
result=0;
switch(ch)    
{
case '+':
result=num1+num2;
break;
case '-':
result=num1-num2;
break;
case '*':
result=num1*num2;
break;
case '/':
result=(float)num1/(float)num2;
break;
default:
printf("Invalid operation.\n");
}
printf("Result: %d %c %d = %f\n",num1,ch,num2,result);
return 0;
}

Output:
Enter first number: 36
Enter second number: 43
Choose operation to perform (+,-,*,/): *
Result: 36 * 43 = 1548.000000