Write a C program to implement menu driven calculator.
int main() {
int operand1, operand2, ch;
float result;
while (1) {
printf("#######Menu##########\n");
printf("1. Addition\n2. Subtraction\n");
printf("3. Division\n4. Multiplication\n");
printf("5. Modulus\n6. Percentage\n");
printf("7. Exit\nEnter your choice:");
scanf("%d", &ch);
/* get input operands from user */
if (ch != 7) {
printf("Enter your operands:");
scanf("%d%d", &operand1, &operand2);
}
switch (ch) {
case 1:
result = operand1 + operand2;
break;
case 2:
result = operand1 - operand2;
break;
case 3:
result = (float)operand1 / operand2;
break;
case 4:
result = operand1 * operand2;
break;
case 5:
result = operand1 % operand2;
break;
case 6:
result = ((float)operand1 / 100) * operand2;
break;
case 7:
exit(0);
default:
printf("Please enter correct option\n");
break;
}
printf("Result: %f\n", result);
}
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
#######Menu##########
1. Addition
2. Subtraction
3. Division
4. Multiplication
5. Modulus
6. Percentage
7. Exit
Enter your choice:1
Enter your operands:100 200
Result: 300.000000
#######Menu##########
1. Addition
2. Subtraction
3. Division
4. Multiplication
5. Modulus
6. Percentage
7. Exit
Enter your choice:2
Enter your operands:200 100
Result: 100.000000
#######Menu##########
1. Addition
2. Subtraction
3. Division
4. Multiplication
5. Modulus
6. Percentage
7. Exit
Enter your choice:6
Enter your operands:10 100
Result: 10.000000
#######Menu##########
1. Addition
2. Subtraction
3. Division
4. Multiplication
5. Modulus
6. Percentage
7. Exit
Enter your choice:5
Enter your operands:10 5
Result: 0.000000
#######Menu##########
1. Addition
2. Subtraction
3. Division
4. Multiplication
5. Modulus
6. Percentage
7. Exit
Enter your choice:4
Enter your operands:128 54
Result: 6912.000000
#######Menu##########
1. Addition
2. Subtraction
3. Division
4. Multiplication
5. Modulus
6. Percentage
7. Exit
Enter your choice:3
Enter your operands:100 20
Result: 5.000000
#######Menu##########
1. Addition
2. Subtraction
3. Division
4. Multiplication
5. Modulus
6. Percentage
7. Exit
Enter your choice:7
#######Menu##########
1. Addition
2. Subtraction
3. Division
4. Multiplication
5. Modulus
6. Percentage
7. Exit
Enter your choice:1
Enter your operands:100 200
Result: 300.000000
#######Menu##########
1. Addition
2. Subtraction
3. Division
4. Multiplication
5. Modulus
6. Percentage
7. Exit
Enter your choice:2
Enter your operands:200 100
Result: 100.000000
#######Menu##########
1. Addition
2. Subtraction
3. Division
4. Multiplication
5. Modulus
6. Percentage
7. Exit
Enter your choice:6
Enter your operands:10 100
Result: 10.000000
#######Menu##########
1. Addition
2. Subtraction
3. Division
4. Multiplication
5. Modulus
6. Percentage
7. Exit
Enter your choice:5
Enter your operands:10 5
Result: 0.000000
#######Menu##########
1. Addition
2. Subtraction
3. Division
4. Multiplication
5. Modulus
6. Percentage
7. Exit
Enter your choice:4
Enter your operands:128 54
Result: 6912.000000
#######Menu##########
1. Addition
2. Subtraction
3. Division
4. Multiplication
5. Modulus
6. Percentage
7. Exit
Enter your choice:3
Enter your operands:100 20
Result: 5.000000
#######Menu##########
1. Addition
2. Subtraction
3. Division
4. Multiplication
5. Modulus
6. Percentage
7. Exit
Enter your choice:7
Calculator program in C
ReplyDeleteIn C language we can design a program to add, subtract, multiply, divide any number, these all operation you can perform by using switch case.