Write a program in C to carry out the arithmetic operations addition, subtraction, multiplication, and division between two variables (using switch case)

C Programming Language / Decision Making of C Language

1313

Program:

#include<stdio.h>
int main()
{
	float value1, value2;
	char operator;
	printf("Type in your expression. \n");
	scanf("%f%c%f",&value1,&operator,&value2);
	switch(operator)
	{
		case '+': printf("%f \n", value1 + value2);
				  break;
		case '-': printf("%f \n", value1 - value2);
				  break;
		case '*': printf("%f \n", value1 * value2);
				  break;
		case '/': if(value2 == 0)
						printf("division by zero. \n");
				  else
						printf("%f \n", value1 / value2);
				  break;
		default:  printf("Unknown Operator \n");
				  break;
	}
	
	getch();
	return 0;
}

Output:

Output 1:
Type in your expression.
2+3
5.000000


Output 2:
Type in your expression.
6*2
12.000000

Explanation:

None

This Particular section is dedicated to Programs only. If you want learn more about C Programming Language. Then you can visit below links to get more depth on this subject.