F.E. PPS Unit 1 Practical 5 solution

Practical 5: Program to Perform All Operations Addition Multiplication Subtraction Division Modulus and Expressions

Problem Statement:

Write a Python program to perform the following operations:

  1. Addition
  2. Subtraction
  3. Multiplication
  4. Division
  5. Modulus
    Also, demonstrate the use of expressions with these operations.

 Solution Code:

				
					# Performing basic operations: Addition, Subtraction, Multiplication, Division, Modulus
 
# Input two numbers from the user
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
 
# Performing Addition
addition = num1 + num2
print("Addition:", addition)
 
# Performing Subtraction
subtraction = num1 - num2
print("Subtraction:", subtraction)
 
# Performing Multiplication
multiplication = num1 * num2
print("Multiplication:", multiplication)
 
# Performing Division
if num2 != 0:
	division = num1 / num2
    print("Division:", division)
else:
    print("Division: Cannot divide by zero!")
 
# Performing Modulus
if num2 != 0:
	modulus = num1 % num2
    print("Modulus:", modulus)
else:
    print("Modulus: Cannot divide by zero!")

				
			

Output:

  • The program will prompt the user to input two numbers.
  • It will then perform the following operations and display the results:
    1. Addition: The sum of the two numbers.
    2. Subtraction: The difference between the two numbers.
    3. Multiplication: The product of the two numbers.
    4. Division: The quotient of the two numbers (if the second number is not zero).
    5. Modulus: The remainder of the division (if the second number is not zero).

Example output for inputs:

				
					Enter the first number: 15
Enter the second number: 94
Addition: 109.0
Subtraction: -79.0
Multiplication: 1410.0
Division: 0.1595744680851064
Modulus: 15.0
Process finished with exit code 0
				
			

If the user enters 0 as the second number for division or modulus:

				
					
Enter the first number: 44
Enter the second number: 0
Addition: 44.0
Subtraction: 44.0
Multiplication: 0.0
Division: Cannot divide by zero! Modulus: Cannot divide by zero!
Process finished with exit code 0
				
			

Explanation:

Below is the explanation of F.E. PPS Unit 1 Practical 5 solution where we have written Python Program to Perform All Operations Addition Multiplication Subtraction Division Modulus and Expressions.

  1. Input of Numbers:
    • The program first prompts the user to enter two numbers using the input() function. These numbers are then converted to float type using float() for operations that may involve decimals.
  2. Addition:
    • The program adds num1 and num2 using the + operator and stores the result in the variable addition. This result is then printed.
  3. Subtraction:
    • The difference between num1 and num2 is calculated using the – operator and stored in subtraction. The result is printed.
  4. Multiplication:
    • The product of num1 and num2 is calculated using the * operator and stored in multiplication. The result is printed.
  5. Division:
    • The program checks if num2 is not zero using an if statement (to avoid division by zero). If valid, the division is performed using /, and the result is stored in division. If num2 is zero, it prints “Cannot divide by zero!”.
  6. Modulus:
    • Similar to division, the modulus operation (%) checks the remainder of num1 divided by num2. Again, it avoids division by zero using the if statement.

 

Key Concept Learned:

  • Arithmetic Operations:
    • Understanding and performing basic arithmetic operations (+, -, *, /, %) in Python.
    • Each operator serves a specific function:
      • + for addition
      • – for subtraction
      • * for multiplication
      • / for division
      • % for modulus (remainder of division)
  • Handling Division by Zero:
    • Using an if statement to check for division by zero before performing division or modulus operations. This ensures that the program doesn’t crash and provides meaningful error messages to the user.
  • Input and Type Conversion:
    • The program demonstrates how to take input from the user using the input() function and convert it into different data types, such as float, to handle non-integer values.
  • Expressions in Python:
    • Understanding how to use arithmetic expressions and how the evaluation of these expressions works in Python. You can perform calculations directly with variables and operators, and Python will handle the precedence automatically.:
Tech Amplifier Final Logo