Final answer:
To develop a menu-driven program in C to perform arithmetic operations and display the result using user-defined functions, follow these steps: create functions for each arithmetic operation, display a menu of options, call the corresponding arithmetic function based on the user's choice, and repeat until the user chooses to exit.
Step-by-step explanation:
To develop a menu-driven program in C to perform arithmetic operations and display the result using user-defined functions, you can follow these steps:
- Create a function for each arithmetic operation: addition, subtraction, multiplication, and division.
- In the main function, display a menu of options for the user to choose from.
- Based on the user's choice, call the corresponding arithmetic function and pass the required input values.
- Display the result obtained from the arithmetic function.
- Repeat steps 2-4 until the user chooses to exit the program.
Here's a sample code snippet:
#include <stdio.h>
int addition(int a, int b) {
return a + b;
}
int subtraction(int a, int b) {
return a - b;
}
int multiplication(int a, int b) {
return a * b;
}
int division(int a, int b) {
return a / b;
}
int main() {
int choice, num1, num2, result;
while (1) {
printf("\\Menu:\\");
printf("1. Addition\\");
printf("2. Subtraction\\");
printf("3. Multiplication\\");
printf("4. Division\\");
printf("5. Exit\\");
printf("Enter your choice: ");
scanf("%d", &choice);
if (choice == 5)
break;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
switch (choice) {
case 1:
result = addition(num1, num2);
break;
case 2:
result = subtraction(num1, num2);
break;
case 3:
result = multiplication(num1, num2);
break;
case 4:
result = division(num1, num2);
break;
default:
printf("Invalid choice!\\");
continue;
}
printf("Result: %d\\", result);
}
return 0;
}