Program1(a)
1. a. Develop a python program to read 2 numbers from the keyboard and perform the basic arithmetic operations based on the choice. (1-Add, 2-Subtract, 3-Multiply, 4-Divide).
Objective of the Program
To understand user input using the keyboard
To apply conditional statements (
if–elif–else)To perform arithmetic operations based on user choice
To understand error handling for division by zero
Logical Construction (Think Before You Code)
Before writing the program, let us clearly understand what the program should do step by step.
The program must accept two numbers from the user.
The program must display a menu showing arithmetic choices.
The user selects one option from the menu.
Based on the selected option:
If the choice is 1, perform addition
If the choice is 2, perform subtraction
If the choice is 3, perform multiplication
If the choice is 4, perform division
While performing division, the program must check for division by zero.
Finally, the program displays the result.
👉 The key idea is:
Input → Decision → Operation → Output
Flowchart (Block Diagram of Logic)
The program starts
Two numbers are read from the user
A choice is taken
Based on the choice, the corresponding arithmetic operation is performed
If division is selected, zero is checked to avoid runtime error
Result is displayed
Program ends
Algorithm
Start
Read first number
aRead second number
bDisplay arithmetic menu
Read user choice
chIf
ch == 1, computea + bElse if
ch == 2, computea - bElse if
ch == 3, computea * bElse if
ch == 4If
b != 0, computea / bElse display error message
Else display invalid choice
Stop
Python Program
⚠️ Students must fill in the missing parts to complete the program
# Program to perform arithmetic operations using choice
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
print("1. Addition")
print("2. Subtraction")
print("3. Multiplication")
print("4. Division")
choice = int(input("Enter your choice: "))
if choice == 1:
result = __________________
print("Addition Result =", result)
elif choice == 2:
result = __________________
print("Subtraction Result =", result)
elif choice == 3:
result = __________________
print("Multiplication Result =", result)
elif choice == 4:
if b != 0:
result = __________________
print("Division Result =", result)
else:
print("Error: Division by zero is not allowed")
else:
print("Invalid choice")
Probable Output
Sample Execution – Addition
Sample Execution – Division by Zero
Important Concepts Used
input()for user inputType conversion using
float()andint()if–elif–elseconditional structureArithmetic operators:
+ - * /
Viva Voce Questions
Why do we use
float()instead ofint()?What is the purpose of the
if–elif–elseladder?What happens if division by zero is not checked?
Difference between
/and//operators?What happens if the user enters an invalid choice?
Can this program work with negative numbers?
Why is choice read as an integer?
Procedure to Execute the Program
Open Python IDLE / VS Code / Google Colab
Create a new Python file
Type the program
Save the file with
.pyextensionRun the program
Enter values when prompted
Google Colab – Online Execution
👉 Click the link below to run the complete working version of this program online:
[https://colab.research.google.com/drive/1sE_JtFsE5LPidxy8944KfWTILLuat5UV?usp=sharing]
Program Explanation
Assignment for Students
Modify the program to include modulus operation
Convert the program using match-case (Python 3.10+)
Rewrite the program using functions
Lab Record Instructions
Right Side of the Record
1. Problem Statement
2. Python Program
Write the following neatly on the right-hand side page:
Left Side of the Record
Write the following neatly on the left-hand side page:
3. Flowchart
4. Algorithm
5. Output
⚠️ Students must write ALL possible outputs.

Comments
Post a Comment