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

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

👁 1,503 Views
💻 Practical Program
📘 Step by Step Learning
Write a program in C to carry out the arithmetic operations addition, subtraction, multiplication, and division between two variables

Program Code

#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

<b>Output 1:</b>
Type in your expression.
2+3
5.000000


<b>Output 2:</b>
Type in your expression.
6*2
12.000000

Explanation

None

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.