C Program to Arithmetic Calculator using switch case Statements

Get a binary arithmetic expression and solve the expression.

Sample Input 1:

12+9

Sample Output 1:

21

Sample Input 2:

4*5

Sample Output 2:

20

Try your Solution

Strongly recommended to Solve it on your own, Don't directly go to the solution given below.

#include<stdio.h> int main() { //write your code here }

Program or Solution

				
			
					
#include<stdio.h>
int main()
{
	int num1,num2,output;
	char operator;
	printf("Enter a binary expression:");
	scanf("%d %c %d",&num1,&operator,&num2);
	switch(operator)
	{
		case '+':
			output=num1+num2;
			break;
		case '-':
			output=num1-num2;
			break;
		case '*':
			output=num1*num2;
			break;
		case '/':
			output=num1/num2;
			break;
		case '%':
			output=num1%num2;
			break;
		default :
			printf("invalid operation");
			break;
	}
	printf("%d",output);
	return 0;
}
			
				
			

Program Explanation

Based on the second character operator, instructions in any one of the case will be executed.

Comments