Learn about Simple Calculator program in C in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.
Contents
Simple Calculator program in C
Source code:
#include<stdio.h>
int main()
{
char ch;
int num1,num2;
printf("Choose the operator(+,-,*,/,%%): ");
scanf("%c",&ch);
printf("Enter two numbers: ");
scanf("%d %d",&num1,&num2);
switch(ch)
{
case '+':
printf("%d + %d =\t%d\n",num1,num2,num1+num2);
break;
case '-':
printf("%d - %d =\t%d\n",num1,num2,num1-num2);
break;
case '*':
printf("%d * %d =\t%d\n",num1,num2,num1*num2);
break;
case '/':
printf("%d / %d =\t%d\n",num1,num2,num1/num2);
break;
case '%':
printf("%d %% %d =\t%d\n",num1,num2,num1%num2);
break;
default:
printf("Error! Invalid Operator.");
}
return 0;
}
Output:
Choose the operator(+,-,*,/,%): +
Enter two numbers: 3 5
3 + 5 = 8
Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.
Similar Codes :
Check Leap Year Using a switch case statement
Check Leap Year Using nested if-else