Experiment 5 – Menu-Driven Calculator
Aim
To create a menu-driven calculator program that performs arithmetic operations (addition, subtraction, multiplication, division, modulus) based on user's choice using switch-case statement in C programming. This experiment introduces students to menu-driven interfaces, switch-case statements, and interactive program design.
Through this experiment, students will learn to design user-friendly interfaces, handle multiple options efficiently, implement error checking (division by zero), and create programs that can perform multiple operations in a single session.
Learning Outcomes
After completing this experiment, students will be able to:
- Design and implement menu-driven user interfaces
- Use switch-case statements for multi-way branching
- Handle user input validation and error cases
- Implement division by zero error checking
- Use do-while loops for repeated menu display
- Create interactive programs that respond to user choices
- Understand the difference between switch-case and if-else for discrete choices
- Write programs with proper error handling and user feedback
Algorithm
The algorithm for a menu-driven calculator involves displaying a menu, reading user choice, performing operations, and optionally repeating:
Algorithm Steps:
- Start: Begin program execution
- Declare Variables: choice, a, b, result, continue_op
- Display Menu: Show available operations (1-5) with descriptions
- Read Choice: Get user's selection
- Read Operands: Get two numbers a and b from user
- Switch on Choice:
- Case 1: result = a + b, display result
- Case 2: result = a - b, display result
- Case 3: result = a * b, display result
- Case 4: Check if b == 0, if yes show error, else result = a / b
- Case 5: Check if b == 0, if yes show error, else result = a % b
- Default: Display "Invalid choice" message
- Ask to Continue: Prompt user if they want another operation
- Loop: If user wants to continue, repeat from step 3
- Stop: End program execution
Procedure
- Include
stdio.hheader file - Declare variables:
choice,a,b,result - Display a menu with operation options (1-5 or +, -, *, /, %)
- Read user's choice
- Read two numbers from user
- Use
switch-caseto perform selected operation:- Case 1: Addition
- Case 2: Subtraction
- Case 3: Multiplication
- Case 4: Division (check for division by zero)
- Case 5: Modulus
- Default: Invalid choice
- Display the result
- Optionally, add a loop to continue operations
Flowchart
START │ ├─→ Display Menu: │ "1. Addition" │ "2. Subtraction" │ "3. Multiplication" │ "4. Division" │ "5. Modulus" │ ├─→ Read choice │ ├─→ Read a, b │ ├─→ SWITCH (choice) │ │ │ ├─→ CASE 1: result = a + b │ │ │ ├─→ CASE 2: result = a - b │ │ │ ├─→ CASE 3: result = a * b │ │ │ ├─→ CASE 4: │ │ IF (b == 0) │ │ Display "Division by zero error" │ │ ELSE │ │ result = a / b │ │ │ ├─→ CASE 5: result = a % b │ │ │ └─→ DEFAULT: Display "Invalid choice" │ ├─→ Display result │ └─→ END
Program Code
#include <stdio.h>
int main() {
int choice;
float a, b, result;
char continue_op;
do {
// Display menu
printf("\n=== MENU-DRIVEN CALCULATOR ===\n");
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Division\n");
printf("5. Modulus\n");
printf("Enter your choice (1-5): ");
scanf("%d", &choice);
// Input two numbers
printf("Enter first number: ");
scanf("%f", &a);
printf("Enter second number: ");
scanf("%f", &b);
// Perform operation based on choice
switch(choice) {
case 1:
result = a + b;
printf("Result: %.2f + %.2f = %.2f\n", a, b, result);
break;
case 2:
result = a - b;
printf("Result: %.2f - %.2f = %.2f\n", a, b, result);
break;
case 3:
result = a * b;
printf("Result: %.2f * %.2f = %.2f\n", a, b, result);
break;
case 4:
if (b == 0) {
printf("Error! Division by zero is not allowed.\n");
} else {
result = a / b;
printf("Result: %.2f / %.2f = %.2f\n", a, b, result);
}
break;
case 5:
if (b == 0) {
printf("Error! Modulus by zero is not allowed.\n");
} else {
result = (int)a % (int)b; // Modulus works with integers
printf("Result: %.0f %% %.0f = %.0f\n", a, b, result);
}
break;
default:
printf("Invalid choice! Please select 1-5.\n");
}
// Ask to continue
printf("\nDo you want to continue? (y/n): ");
scanf(" %c", &continue_op);
} while (continue_op == 'y' || continue_op == 'Y');
printf("\nThank you for using the calculator!\n");
return 0;
}Sample Input and Output
Sample 1:
Input:
=== MENU-DRIVEN CALCULATOR === 1. Addition 2. Subtraction 3. Multiplication 4. Division 5. Modulus Enter your choice (1-5): 1 Enter first number: 15.5 Enter second number: 8.3
Output:
Result: 15.50 + 8.30 = 23.80
Sample 2:
Input:
=== MENU-DRIVEN CALCULATOR === 1. Addition 2. Subtraction 3. Multiplication 4. Division 5. Modulus Enter your choice (1-5): 4 Enter first number: 20 Enter second number: 0
Output:
Error! Division by zero is not allowed.
Sample 3:
Input:
=== MENU-DRIVEN CALCULATOR === 1. Addition 2. Subtraction 3. Multiplication 4. Division 5. Modulus Enter your choice (1-5): 3 Enter first number: 7 Enter second number: 6
Output:
Result: 7.00 * 6.00 = 42.00
Use Case / Real-world Relevance
Menu-driven programs are widely used in real-world applications:
- ATM Machines: Menu-driven interface for banking operations
- Restaurant Ordering Systems: Menu selection for food items and customization
- Library Management: Menu options for book operations (add, search, issue, return)
- Student Information Systems: Menu for different administrative functions
- E-commerce Applications: Product categories and filtering options
- Settings Menus: Configuration options in software applications
- Game Menus: Start, settings, load game, exit options
- Database Management: CRUD operations (Create, Read, Update, Delete) presented as menu
The switch-case statement is ideal for handling multiple discrete choices, making code more readable and maintainable than multiple if-else statements. This pattern is fundamental in user interface design. Menu-driven programs provide intuitive interfaces that guide users through available options, making software more accessible and user-friendly.
Viva Questions
Q1: What is the difference between switch-case and if-else ladder?
Switch-case works with discrete values (integers, characters, enums) and is more efficient for multiple choices. If-else can handle ranges, complex conditions, and any boolean expression. Switch is cleaner for menu-driven programs with numbered options, while if-else is more flexible for complex conditions.
Q2: Why is break statement important in switch-case?
Without break, execution "falls through" to the next case, executing all subsequent cases until a break is encountered. This is usually a bug but can be intentional when multiple cases share the same code. Always use break unless you intentionally want fall-through behavior.
Q3: Why do we check for division by zero in case 4 and 5?
Division by zero causes undefined behavior (may crash the program). Modulus by zero also causes undefined behavior. We must validate input before performing these operations to prevent program errors and provide user-friendly error messages instead of crashes.
Q4: Can we use if-else instead of switch-case for this program?
Yes, but switch-case is more appropriate here because we're checking discrete integer values (1-5). Switch is more readable, potentially faster (compiler can optimize with jump tables), and clearly shows we're handling multiple discrete choices. If-else would work but is less elegant for this use case.
Q5: Why do we use do-while loop instead of while loop?
Do-while ensures the menu is displayed at least once, which is what we want for a calculator. With while loop, if the condition is false initially, the menu wouldn't display. Do-while is perfect for menu-driven programs where you want to show the menu first, then check if user wants to continue.
Q6: What happens if user enters a choice outside 1-5?
The default case handles invalid choices, displaying an error message. The program doesn't crash but informs the user of the invalid input. This is good error handling - graceful degradation rather than program termination.
Q7: Can we add more operations to this calculator? How?
Yes, add more cases to the switch statement. For example, case 6 for power, case 7 for square root, etc. Update the menu display to show new options. The switch-case structure makes it easy to extend functionality without modifying existing code.
Q8: Why do we use float for operands instead of int?
Using float allows decimal numbers in calculations, making the calculator more versatile. Division results in decimal values, so float ensures accurate results. If we used int, division would truncate decimal parts. For modulus operation, we cast to int since modulus works only with integers.
Related Links
🔹 Author: Dr. J. Siva Ramakrishna
🔹 Institution: Narayana Engineering College, Gudur
🔹 Last Updated: 9 January 2026