Home C Programming Language / 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,506 Views
💻 Practical Program
📘 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;
}


                        

🖥 Program 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
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.