Python Programming
Arithmetic, Assignment & Relational Operators — Questions
1. Compound Interest Calculator
Write a Python program to calculate Compound Interest. Accept principal, rate, time, and
compounding periods. Display final amount, compound interest, and check if it is greater than
simple interest.
2. Quadratic Equation Discriminant
Write a Python program to find the discriminant of ax² + bx + c = 0. Accept a, b, c and check
whether roots are real, equal, or distinct.
3. Distance Between Two Points
Write a Python program to calculate the distance between (x1,y1) and (x2,y2). Also find the
midpoint and check if origin is closer to point 1.
4. Electricity Bill Calculator
Write a Python program to calculate electricity bill using slab rates: Rs.2.50 (0-100 units), Rs.4.00
(101-300), Rs.6.00 (301+), Rs.50 fixed charge, and 5% tax. Check if total exceeds Rs.500.
5. Speed, Distance & Time
Write a Python program that accepts distance (km) and time (minutes). Calculate speed in km/h
and m/s, time for 100 km, and check if speed exceeds 60 km/h.
6. Profit and Loss Calculator
Write a Python program to find profit or loss. Accept cost price and selling price. Display amount,
percentage, and check if it is profit, loss, or break-even.
7. Currency Converter
Write a Python program to convert USD to INR (83.5), EUR (0.92), and GBP (0.79). Show INR after
2% bank fee and check if it exceeds Rs.10,000.
8. Simple Interest Calculator
Write a Python program to calculate Simple Interest using SI = (P x R x T) / 100.
9. Area of a Rectangle
Write a Python program to calculate area of a rectangle using Area = length x width.
10. Perimeter of a Rectangle
Write a Python program to calculate perimeter of a rectangle using Perimeter = 2 x (length + width).
11. Celsius to Fahrenheit
Write a Python program to convert Celsius to Fahrenheit using F = (C x 9/5) + 32.
12. Check Even or Odd
Write a Python program to check whether a number is even or odd using modulus and relational
operators.
13. Compare Two Numbers
Write a Python program to compare two numbers and display whether the first is greater than, less
than, or equal to the second.
14. Voting Eligibility
Write a Python program to check whether a person is eligible to vote. Age must be 18 or above.
15. Area of a Circle
Write a Python program to calculate area of a circle using pi = 3.14159 and Area = pi x r².
16. Average of Three Numbers
Write a Python program to find the average of three numbers using Average = (n1 + n2 + n3) / 3.
17. Swap Two Numbers
Write a Python program to swap two numbers using tuple assignment and display values before
and after.
18. Compound Assignment Operators
Write a Python program to demonstrate +=, -=, *=, and //= operators on a number entered by the
user.
19. Positive, Negative, or Zero
Write a Python program to check whether a number is positive, negative, or zero using >, <, and ==
operators.
20. BMI Calculator
Write a Python program to calculate BMI = weight / height². Check if the result is in the healthy
range (18.5 to 24.9).
Python Programming
Arithmetic, Assignment & Relational Operators — Programs & Output
Program 1: Compound Interest Calculator
Code:
principal = float(input("Enter principal amount: "))
rate = float(input("Enter annual rate of interest (%): "))
time = float(input("Enter time in years: "))
n = int(input("Enter compounding periods per year: "))
amount = principal * ((1 + (rate / 100) / n) ** (n * time))
compound_interest = amount - principal
print("Final Amount: Rs.", round(amount, 2))
print("Compound Interest: Rs.", round(compound_interest, 2))
print("More than SI?", compound_interest > (principal * rate * time) / 100)
Output:
Enter principal amount: 10000
Enter annual rate of interest (%): 8
Enter time in years: 3
Enter compounding periods per year: 4
Final Amount: Rs. 12682.42
Compound Interest: Rs. 2682.42
More than SI? True
Program 2: Quadratic Equation Discriminant
Code:
a = float(input("Enter coefficient a: "))
b = float(input("Enter coefficient b: "))
c = float(input("Enter coefficient c: "))
discriminant = (b ** 2) - (4 * a * c)
print("Discriminant (D) =", discriminant)
print("Has real roots? ", discriminant >= 0)
print("Has equal roots? ", discriminant == 0)
print("Has distinct roots?", discriminant > 0)
Output:
Enter coefficient a: 1
Enter coefficient b: -5
Enter coefficient c: 6
Discriminant (D) = 1.0
Has real roots? True
Has equal roots? False
Has distinct roots? True
Program 3: Distance Between Two Points
Code:
x1 = float(input("Enter x1: "))
y1 = float(input("Enter y1: "))
x2 = float(input("Enter x2: "))
y2 = float(input("Enter y2: "))
distance = ((x2 - x1) ** 2 + (y2 - y1) ** 2) ** 0.5
mid_x = (x1 + x2) / 2
mid_y = (y1 + y2) / 2
print("Distance:", round(distance, 4))
print("Midpoint: (", mid_x, ",", mid_y, ")")
print("Origin closer to P1?", (x1**2+y1**2)**0.5 < (x2**2+y2**2)**0.5)
Output:
Enter x1: 1
Enter y1: 2
Enter x2: 4
Enter y2: 6
Distance: 5.0
Midpoint: ( 2.5 , 4.0 )
Origin closer to P1? True
Program 4: Electricity Bill Calculator
Code:
units = int(input("Enter units consumed: "))
bill = 50
consumed = units
slab1 = min(consumed, 100)
bill += slab1 * 2.50
consumed -= slab1
slab2 = min(consumed, 200)
bill += slab2 * 4.00
consumed -= slab2
bill += consumed * 6.00
tax = bill * 0.05
total = bill + tax
print("Base Bill: Rs.", round(bill, 2))
print("Tax (5%): Rs.", round(tax, 2))
print("Total: Rs.", round(total, 2))
print("Exceeds Rs.500?", total > 500)
Output:
Enter units consumed: 350
Base Bill: Rs. 1400.0
Tax (5%): Rs. 70.0
Total: Rs. 1470.0
Exceeds Rs.500? True
Program 5: Speed, Distance & Time
Code:
distance_km = float(input("Enter distance in km: "))
time_min = float(input("Enter time in minutes: "))
speed_kmph = distance_km / (time_min / 60)
speed_mps = speed_kmph * (1000 / 3600)
time_for_100km = 100 / speed_kmph * 60
print("Speed:", round(speed_kmph, 2), "km/h")
print("Speed:", round(speed_mps, 2), "m/s")
print("Time for 100 km:", round(time_for_100km, 2), "min")
print("Exceeds 60 km/h?", speed_kmph > 60)
Output:
Enter distance in km: 45
Enter time in minutes: 30
Speed: 90.0 km/h
Speed: 25.0 m/s
Time for 100 km: 66.67 min
Exceeds 60 km/h? True
Program 6: Profit and Loss Calculator
Code:
cost_price = float(input("Enter cost price: "))
selling_price = float(input("Enter selling price: "))
difference = selling_price - cost_price
percent = (difference / cost_price) * 100
print("Difference: Rs.", abs(difference))
print("Percentage:", round(abs(percent), 2), "%")
print("Is profit? ", selling_price > cost_price)
print("Is loss? ", selling_price < cost_price)
print("Is break-even?", selling_price == cost_price)
Output:
Enter cost price: 800
Enter selling price: 1000
Difference: Rs. 200.0
Percentage: 25.0 %
Is profit? True
Is loss? False
Is break-even? False
Program 7: Currency Converter
Code:
amount_usd = float(input("Enter amount in USD: "))
amount_inr = amount_usd * 83.5
amount_eur = amount_usd * 0.92
amount_gbp = amount_usd * 0.79
amount_inr_fee = amount_inr - (amount_inr * 0.02)
print("INR:", round(amount_inr, 2))
print("EUR:", round(amount_eur, 2))
print("GBP:", round(amount_gbp, 2))
print("INR after 2% fee:", round(amount_inr_fee, 2))
print("Exceeds INR 10000?", amount_inr > 10000)
Output:
Enter amount in USD: 150
INR: 12525.0
EUR: 138.0
GBP: 118.5
INR after 2% fee: 12274.5
Exceeds INR 10000? True
Program 8: Simple Interest Calculator
Code:
principal = float(input("Enter principal: "))
rate = float(input("Enter rate (%): "))
time = float(input("Enter time (years): "))
si = (principal * rate * time) / 100
print("Simple Interest:", si)
Output:
Enter principal: 1000
Enter rate (%): 5
Enter time (years): 2
Simple Interest: 100.0
Program 9: Area of a Rectangle
Code:
length = float(input("Enter length: "))
width = float(input("Enter width: "))
area = length * width
print("Area:", area)
Output:
Enter length: 6
Enter width: 4
Area: 24.0
Program 10: Perimeter of a Rectangle
Code:
length = float(input("Enter length: "))
width = float(input("Enter width: "))
perimeter = 2 * (length + width)
print("Perimeter:", perimeter)
Output:
Enter length: 6
Enter width: 4
Perimeter: 20.0
Program 11: Celsius to Fahrenheit
Code:
celsius = float(input("Enter temperature in Celsius: "))
fahrenheit = (celsius * 9/5) + 32
print("Fahrenheit:", fahrenheit)
Output:
Enter temperature in Celsius: 100
Fahrenheit: 212.0
Program 12: Check Even or Odd
Code:
num = int(input("Enter a number: "))
is_even = (num % 2 == 0)
print("Is even?", is_even)
Output:
Enter a number: 7
Is even? False
Program 13: Compare Two Numbers
Code:
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("a > b :", a > b)
print("a < b :", a < b)
print("a == b:", a == b)
Output:
Enter first number: 10
Enter second number: 5
a > b : True
a < b : False
a == b: False
Program 14: Voting Eligibility
Code:
age = int(input("Enter your age: "))
eligible = age >= 18
print("Eligible to vote?", eligible)
Output:
Enter your age: 20
Eligible to vote? True
Program 15: Area of a Circle
Code:
pi = 3.14159
radius = float(input("Enter the radius: "))
area = pi * radius ** 2
print("Area:", round(area, 2))
Output:
Enter the radius: 7
Area: 153.94
Program 16: Average of Three Numbers
Code:
n1 = float(input("Enter first number: "))
n2 = float(input("Enter second number: "))
n3 = float(input("Enter third number: "))
average = (n1 + n2 + n3) / 3
print("Average:", average)
Output:
Enter first number: 10
Enter second number: 20
Enter third number: 30
Average: 20.0
Program 17: Swap Two Numbers
Code:
x = int(input("Enter first value: "))
y = int(input("Enter second value: "))
print("Before: x =", x, "y =", y)
x, y = y, x
print("After: x =", x, "y =", y)
Output:
Enter first value: 5
Enter second value: 9
Before: x = 5 y = 9
After: x = 9 y = 5
Program 18: Compound Assignment Operators
Code:
num = int(input("Enter a number: "))
print("Original:", num)
num += 10
print("After += 10:", num)
num -= 5
print("After -= 5: ", num)
num *= 2
print("After *= 2: ", num)
num //= 3
print("After //= 3:", num)
Output:
Enter a number: 8
Original: 8
After += 10: 18
After -= 5: 13
After *= 2: 26
After //= 3: 8
Program 19: Positive, Negative, or Zero
Code:
num = float(input("Enter a number: "))
print("Is positive?", num > 0)
print("Is negative?", num < 0)
print("Is zero? ", num == 0)
Output:
Enter a number: -7
Is positive? False
Is negative? True
Is zero? False
Program 20: BMI Calculator
Code:
weight = float(input("Enter weight in kg: "))
height = float(input("Enter height in meters: "))
bmi = weight / (height ** 2)
bmi = round(bmi, 2)
print("BMI:", bmi)
print("Healthy BMI?", 18.5 <= bmi <= 24.9)
Output:
Enter weight in kg: 70
Enter height in meters: 1.75
BMI: 22.86
Healthy BMI? True