.
Shri Vile Parle Kelavani Mandal's
INSTITUTE OF TECHNOLOGY
DHULE (M.S.)
DEPARMENT OF APPLIED SCIENCE AND HUMANITY
Subject : PPS Lab Subject Code : 24AF1000ES107L
Class: FY Expt. No. : 05
Title: Menu driven program to perform simple arithmetic operations based on user’s choice.
Aim : To write and execute a menu driven C program that performs simple arithmetic
operations (addition, subtraction, multiplication, division) based on the user’s
choice using operator signs.
Objective: To practice the use of conditional statements (switch or if-else).
To understand how to design a menu-driven program.
To perform arithmetic operations interactively.
Theory: A menu-driven program is an interactive program that provides the
user with a set of choices (menu) and performs a task based on the
selected option. In this case, the menu allows the user to select an
arithmetic operation such as addition, subtraction, multiplication, or
division by entering the respective operator (+, -, *, /).
Key Concepts Used:
1. Input/Output in C:
o scanf() is used to accept user input.
o printf() is used to display results and messages to
the user.
2. Arithmetic Operators in C:
o + → Addition
o - → Subtraction
o * → Multiplication
o / → Division (with denominator check to avoid division
by zero)
3. Decision-Making (Control Statements):
o A switch-case statement is used to handle multiple
choices efficiently.
o Based on the operator entered by the user, the
corresponding case is executed.
o If none of the cases match, the default case is executed,
displaying "Invalid operator".
4. Type Casting in Division:
o Since division of two integers can truncate results, one
operand is converted to float ((float)a/b) to get
accurate decimal results.
5. Error Handling:
o Division by zero is invalid in mathematics, so the
program includes a check (if (b != 0)) before
performing division.
Working Principle:
• The program takes two numbers and an operator symbol as
input.
• Using the menu (operator symbol) and decision-making
structure, the program performs the corresponding arithmetic
operation.
• Finally, it displays the result back to the user.
Algorithm:
1. Start.
2. Read two numbers a and b from the user.
3. Display a menu asking the user to enter a choice of operation
(+, -, *, /).
4. Read the operator symbol.
5. Use switch-case (or if-else) to perform the operation:
o If + → compute sum = a + b.
o If - → compute difference = a - b.
o If * → compute product = a * b.
o If / → compute quotient = a / b (check if b != 0).
o Otherwise → print "Invalid Choice".
6. Display the result.
7. Stop.
Flowchart:
┌─────────────┐
│ START │
└──────┬──────┘
│
┌─────────▼─────────┐
│ Read a, b │
└─────────┬─────────┘
│
┌─────────▼─────────┐
│ Display menu: │
│ + - * / │
└─────────┬─────────┘
│
┌─────────▼─────────┐
│ Read operator op │
└─────────┬─────────┘
│
┌──────── ─▼──────────┐
┌─────▶│ op == '+' ? │──Yes──▶ result = a+b
│ └─────────┬──────────┘
│ │No
│ ┌─────────▼──────────┐
│ │ op == '-' ? │──Yes──▶ result = a-b
│ └─────────┬──────────┘
│ │No
│ ┌─────────▼──────────┐
│ │ op == '*' ? │──Yes──▶ result = a*b
│ └─────────┬──────────┘
│ │No
│ ┌─────────▼──────────┐
│ │ op == '/' ? │──Yes──▶ if b!=0 → result=a/b
│ └─────────┬──────────┘
│ │No
│ ┌─────────▼──────────┐
│ │ Invalid Choice │
│ └─────────┬──────────┘
│ │
└────────────────┘
│
┌─────────▼─────────┐
│ Print result │
└─────────┬─────────┘
│
┌──────▼──────┐
│ STOP │
└─────────────┘
C Program:
#include <stdio.h>
int main() {
Code: int a, b;
char op;
float result;
// Input two numbers
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
// Display menu
printf("Choose operation (+, -, *, /): ");
scanf(" %c", &op);
// Perform operation
switch(op) {
case '+':
result = a + b;
printf("Result = %.2f\n", result);
break;
case '-':
result = a - b;
printf("Result = %.2f\n", result);
break;
case '*':
result = a * b;
printf("Result = %.2f\n", result);
break;
case '/':
if(b != 0) {
result = (float)a / b;
printf("Result = %.2f\n", result);
} else {
printf("Error! Division by zero not allowed.\n");
}
break;
default:
printf("Invalid operator!\n");
}
return 0;
}
Sample Input/Output:
Case 1:
Enter two numbers: 10 5
Choose operation (+, -, *, /): *
Result = 50.00
Case 2:
Enter two numbers: 25 5
Choose operation (+, -, *, /): /
Result = 5.00