Decode School

Decode School - The School for Problem Solvers

Welcome to my blog. Subscribe and get my latest blog post in your inbox.

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

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.