Please fill in the missing lines of code to complete the following menu driven C program to
implement a calculator.
Logic:
.
1- Accept two numbers x and y as input.
- Accept a value as choice.
• - If the choice is 1, add the two numbers and store x+y in the variable result
. - If the choice is 2, subtract the two numbers and store x-y in the variable result
• - If the choice is 3, multiply the two numbers and store x*y in the variable result
• - If the choice is 4, divide the two numbers and store x/y in the variable result.
Answers
Explanation:
swPlease fill in the missing lines of code to complete the following menu driven C program to implement a calculator.
Logic:
- Accept two numbers x and y as input.
- Accept a value as choice.
- If the choice is 1, add the two numbers and store x+y in the variable result
- If the choice is 2, subtract the two numbers and store x-y in the variable result
- If the choice is 3, multiply the two numbers and store x*y in the variable result
- If the choice is 4, divide the two numbers and store x/y in the variable result
Answer:
Explanation:
Implementing a calculator :
Logic:
1- Accept two numbers x and y as input.
- Accept a value as choice.
• - If the choice is 1, add the two numbers and store x+y in the variable result
. - If the choice is 2, subtract the two numbers and store x-y in the variable result
• - If the choice is 3, multiply the two numbers and store x*y in the variable result
• - If the choice is 4, divide the two numbers and store x/y in the variable result.
Program to implement a calculator :
#include <stdio.h>
int main() {
int x, y;
int choice;
printf("Enter 1. for "+", 2. for "-", 3. for "*", 4. for "/" ");
scanf("%c", &choice);
printf("Enter two operands: ");
scanf("%d %d", &x, &y);
switch (choice) {
case '1' :
printf("%d + %d = %d", x, y, x + y);
break;
case '-':
printf("%d - %d = %d", x , y, x - y);
break;
case '*':
printf("%d * %d = %d", x, y, x * y);
break;
case '/':
printf("%d / %d = %d", x, y, x/y);
break;
default:
printf("Error, operator is not correct");
}
#SPJ3