C Program to Arithmetic Calculator using switch case Statements

Description

Get a binary arthmetic expression and solve the expression.
Input:
12+9
Ouput:
21
Input:
4*5
Output:
20

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;
}

Explanation

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