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.

  1. The program must accept two numbers from the user.

  2. The program must display a menu showing arithmetic choices.

  3. The user selects one option from the menu.

  4. 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

  5. While performing division, the program must check for division by zero.

  6. Finally, the program displays the result.

👉 The key idea is:
Input → Decision → Operation → Output



Flowchart (Block Diagram of Logic)




Explanation of Flowchart
  • 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

  1. Start

  2. Read first number a

  3. Read second number b

  4. Display arithmetic menu

  5. Read user choice ch

  6. If ch == 1, compute a + b

  7. Else if ch == 2, compute a - b

  8. Else if ch == 3, compute a * b

  9. Else if ch == 4

    • If b != 0, compute a / b

    • Else display error message

  10. Else display invalid choice

  11. 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

Enter first number: 10
Enter second number: 5
Enter your choice: 1
Addition Result = 15

Sample Execution – Division by Zero

Enter first number: 8
Enter second number: 0
Enter your choice: 4
Error: Division by zero is not allowed


Important Concepts Used

  • input() for user input

  • Type conversion using float() and int()

  • if–elif–else conditional structure

  • Arithmetic operators: + - * /



Viva Voce Questions

  1. Why do we use float() instead of int()?

  2. What is the purpose of the if–elif–else ladder?

  3. What happens if division by zero is not checked?

  4. Difference between / and // operators?

  5. What happens if the user enters an invalid choice?

  6. Can this program work with negative numbers?

  7. Why is choice read as an integer?



Procedure to Execute the Program

  1. Open Python IDLE / VS Code / Google Colab

  2. Create a new Python file

  3. Type the program

  4. Save the file with .py extension

  5. Run the program

  6. 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.



-:END:-

Comments

Popular posts from this blog