Program to make a simple calculator to add, subtract, multiply or divide using switch case in C

Source Code

// 29. Program to make a simple calculator to add, subtract, multiply or divide using switch case
// Arpan Das - CST 2nd Year (VOCLET)

#include <stdio.h>

void main () {
	int ch;
	float x, y;
	//clrscr();

	printf("\t\t..:: simple calculator to add, subtract, multiply or divide using switch case ::..\n\n");

	do {
		printf("\n1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n5. Exit\n");
		printf("\nPlease enter your choice: ");
		scanf("%d", &ch);

		switch (ch) {
			case 1:
				printf("\nEnter operand1 and operand2 respectively: ");
				scanf("%f%f", &x, &y);
				printf("\n\tResult: %.3f\n", x+y);
				break;
			case 2:
				printf("\nEnter operand1 and operand2 respectively: ");
				scanf("%f%f", &x, &y);
				printf("\n\tResult: %.3f\n", x-y);
				break;
			case 3:
				printf("\nEnter operand1 and operand2 respectively: ");
				scanf("%f%f", &x, &y);
				printf("\n\tResult: %.3f\n", x*y);
				break;
			case 4:
				printf("\nEnter operand1 and operand2 respectively: ");
				scanf("%f%f", &x, &y);
				printf("\n\tResult: %.3f\n", x/y);
				break;
			default:
				break;
		}
	} while (ch >= 1 && ch <= 4);
}

Output

		..:: simple calculator to add, subtract, multiply or divide using switch case ::..


1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit

Please enter your choice: 1

Enter operand1 and operand2 respectively: 12 14

	Result: 26.000

1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit

Please enter your choice: 2

Enter operand1 and operand2 respectively: 20 5

	Result: 15.000

1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit

Please enter your choice: 3

Enter operand1 and operand2 respectively: 5 6

	Result: 30.000

1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit

Please enter your choice: 4

Enter operand1 and operand2 respectively: 100 25

	Result: 4.000

1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit

Please enter your choice: 5

Leave a Reply

Note: Your email address will not be published. Required fields are marked *