0% found this document useful (0 votes)
3 views10 pages

Python Programs

Uploaded by

Kiruthika Kannan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views10 pages

Python Programs

Uploaded by

Kiruthika Kannan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Python Programming

Arithmetic, Assignment & Relational Operators


QUESTIONS

1 Compound Interest Calculator


Write a Python program to calculate Compound Interest. Accept the principal amount, annual
rate of interest (%), time in years, and number of compounding periods per year from the user.
Display the final amount, compound interest earned, and check whether the compound interest
is greater than simple interest for the same inputs.

2 Quadratic Equation Discriminant


Write a Python program to find the discriminant of a quadratic equation ax² + bx + c = 0. Accept
coefficients a, b, and c from the user. Display the discriminant value and use relational
operators to check whether the equation has real roots, equal roots, or distinct roots.

3 Distance Between Two Points


Write a Python program to calculate the distance between two points (x1, y1) and (x2, y2) in a
2D plane. Accept all four coordinates as input. Also compute the midpoint and use a relational
operator to check whether the origin is closer to point 1 than to point 2.

4 Electricity Bill Calculator


Write a Python program to calculate an electricity bill based on slab rates: Rs. 2.50/unit for the
first 100 units, Rs. 4.00/unit for 101–300 units, and Rs. 6.00/unit above 300 units, plus a fixed
charge of Rs. 50. Apply 5% tax and display the base bill, tax, and total bill. Check if the total
exceeds Rs. 500.

5 Speed, Distance & Time with Unit Conversion


Write a Python program that accepts distance in km and time in minutes. Calculate speed in
km/h and m/s, and the time required to cover 100 km at that speed. Use a relational operator to
check whether the speed exceeds the 60 km/h speed limit.

6 Profit and Loss Calculator


Write a Python program to calculate profit or loss. Accept the cost price and selling price from
the user. Display the profit/loss amount and percentage. Use relational operators to separately
check whether the transaction resulted in a profit, a loss, or a break-even.

7 Currency Converter
Write a Python program to convert a given USD amount to INR (83.5), EUR (0.92), and GBP
(0.79). Also compute the INR value after deducting a 2% bank fee. Use a relational operator to
check whether the converted INR amount exceeds Rs. 10,000.
8 Simple Interest Calculator
Write a Python program to calculate Simple Interest. Accept the principal, rate of interest (%),
and time in years from the user. Display the simple interest using the formula SI = (P × R × T) /
100.

9 Area of a Rectangle
Write a Python program to calculate the area of a rectangle. Accept the length and width from
the user and display the area using the formula: Area = length × width.

10 Perimeter of a Rectangle
Write a Python program to calculate the perimeter of a rectangle. Accept the length and width
from the user and display the perimeter using the formula: Perimeter = 2 × (length + width).

11 Celsius to Fahrenheit Conversion


Write a Python program to convert temperature from Celsius to Fahrenheit. Accept the
temperature in Celsius from the user and display the equivalent Fahrenheit value using the
formula: F = (C × 9/5) + 32.

12 Check Even or Odd


Write a Python program to check whether a given number is even or odd. Accept an integer
from the user, use the modulus operator (%) to find the remainder when divided by 2, and
display the result using a relational operator.

13 Compare Two Numbers


Write a Python program to compare two numbers entered by the user. Use relational operators
to check and display: whether the first number is greater than the second, whether the first is
less than the second, and whether both numbers are equal.

14 Voting Eligibility Check


Write a Python program to check voting eligibility. Accept a person's age as input and use a
relational operator (>=) to determine and display whether the person is eligible to vote (age
must be 18 or above).

15 Area of a Circle
Write a Python program to calculate the area of a circle. Accept the radius from the user. Use pi
= 3.14159 and compute the area using the formula: Area = π × r². Display the result rounded to
2 decimal places.

16 Average of Three Numbers


Write a Python program to calculate the average of three numbers. Accept three numbers from
the user and display the average using the formula: Average = (n1 + n2 + n3) / 3.

17 Swap Two Numbers


Write a Python program to swap the values of two variables using Python's tuple assignment
operator. Accept two integers from the user, display the values before and after swapping.

18 Compound Assignment Operators


Write a Python program to demonstrate compound assignment operators. Accept a number
from the user, then apply and display the result after each of the following operations in
sequence: += 10, -= 5, *= 2, and //= 3.

19 Check Positive, Negative, or Zero


Write a Python program to check whether a number is positive, negative, or zero. Accept a
number as input and use relational operators (>, <, ==) to individually check and display each
condition.

20 BMI Calculator
Write a Python program to calculate Body Mass Index (BMI). Accept weight in kg and height in
meters from the user. Compute BMI = weight / height², round to 2 decimal places, and use a
relational operator to check whether the BMI falls in the healthy range (18.5 to 24.9).
Python Programming
Arithmetic, Assignment & Relational Operators
PROGRAMS WITH 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 number of times interest compounds per year: "))
amount = principal * ((1 + (rate / 100) / n) ** (n * time))
compound_interest = amount - principal
print(f"Final Amount: Rs. {round(amount, 2)}")
print(f"Compound Interest: Rs. {round(compound_interest, 2)}")
print(f"Profit more than SI? {compound_interest > (principal * rate * time) / 100}")

SAMPLE INPUT
10000
8
3
4

OUTPUT
Enter principal amount: 10000
Enter annual rate of interest (%): 8
Enter time in years: 3
Enter number of times interest compounds per year: 4
Final Amount: Rs. 12682.42
Compound Interest: Rs. 2682.42
Profit 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(f"Discriminant (D) = {discriminant}")
print(f"Has real roots? {discriminant >= 0}")
print(f"Has equal roots? {discriminant == 0}")
print(f"Has distinct roots? {discriminant > 0}")

SAMPLE INPUT
1
-5
6

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(f"Distance: {round(distance, 4)}")
print(f"Midpoint: ({mid_x}, {mid_y})")
print(f"Is origin closer to point1 than point2? {(x1**2 + y1**2)**0.5 < (x2**2 +
y2**2)**0.5}")

SAMPLE INPUT
1
2
4
6

OUTPUT
Enter x1: 1
Enter y1: 2
Enter x2: 4
Enter y2: 6
Distance: 5.0
Midpoint: (2.5, 4.0)
Is origin closer to point1 than point2? True

Program 4: Electricity Bill Calculator


CODE
units = int(input("Enter units of electricity consumed: "))
rate_slab1 = 2.50
rate_slab2 = 4.00
rate_slab3 = 6.00
fixed_charge = 50
bill = fixed_charge
consumed = units
slab1_units = min(consumed, 100)
bill += slab1_units * rate_slab1
consumed -= slab1_units
slab2_units = min(consumed, 200)
bill += slab2_units * rate_slab2
consumed -= slab2_units
bill += consumed * rate_slab3
tax = bill * 0.05
total = bill + tax
print(f"Base Bill: Rs. {round(bill, 2)}")
print(f"Tax (5%): Rs. {round(tax, 2)}")
print(f"Total Bill: Rs. {round(total, 2)}")
print(f"Exceeds budget of Rs.500? {total > 500}")

SAMPLE INPUT
350

OUTPUT
Enter units of electricity consumed: 350
Base Bill: Rs. 1400.0
Tax (5%): Rs. 70.0
Total Bill: Rs. 1470.0
Exceeds budget of Rs.500? True

Program 5: Speed, Distance & Time


CODE
distance_km = float(input("Enter distance in kilometers: "))
time_min = float(input("Enter time taken in minutes: "))
speed_kmph = distance_km / (time_min / 60)
speed_mps = speed_kmph * (1000 / 3600)
time_for_100km = 100 / speed_kmph * 60
print(f"Speed: {round(speed_kmph, 2)} km/h")
print(f"Speed in m/s: {round(speed_mps, 2)} m/s")
print(f"Time for 100 km: {round(time_for_100km, 2)} minutes")
print(f"Exceeds speed limit (60 km/h)? {speed_kmph > 60}")

SAMPLE INPUT
45
30

OUTPUT
Enter distance in kilometers: 45
Enter time taken in minutes: 30
Speed: 90.0 km/h
Speed in m/s: 25.0 m/s
Time for 100 km: 66.67 minutes
Exceeds speed limit (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(f"Cost Price: Rs. {cost_price}")
print(f"Selling Price: Rs. {selling_price}")
print(f"Profit Amount: Rs. {abs(difference)}")
print(f"Profit %: {round(abs(percent), 2)}%")
print(f"Is profit? {selling_price > cost_price}")
print(f"Is loss? {selling_price < cost_price}")
print(f"Is break-even? {selling_price == cost_price}")

SAMPLE INPUT
800
1000

OUTPUT
Enter cost price: 800
Enter selling price: 1000
Cost Price: Rs. 800.0
Selling Price: Rs. 1000.0
Profit Amount: Rs. 200.0
Profit %: 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: "))
usd_to_inr = 83.5
usd_to_eur = 0.92
usd_to_gbp = 0.79
amount_inr = amount_usd * usd_to_inr
amount_eur = amount_usd * usd_to_eur
amount_gbp = amount_usd * usd_to_gbp
amount_inr_with_fee = amount_inr - (amount_inr * 0.02)
print(f"USD {amount_usd} = INR {round(amount_inr, 2)}")
print(f"USD {amount_usd} = EUR {round(amount_eur, 2)}")
print(f"USD {amount_usd} = GBP {round(amount_gbp, 2)}")
print(f"INR after 2% bank fee: {round(amount_inr_with_fee, 2)}")
print(f"Worth more than INR 10000? {amount_inr > 10000}")

SAMPLE INPUT
150

OUTPUT
Enter amount in USD: 150
USD 150.0 = INR 12525.0
USD 150.0 = EUR 138.0
USD 150.0 = GBP 118.5
INR after 2% bank fee: 12274.5
Worth more than INR 10000? True

Program 8: Simple Interest Calculator


CODE
principal = float(input("Enter principal amount: "))
rate = float(input("Enter rate of interest (%): "))
time = float(input("Enter time (years): "))
si = (principal * rate * time) / 100
print("Simple Interest:", si)

SAMPLE INPUT
1000
5
2

OUTPUT
Enter principal amount: 1000
Enter rate of interest (%): 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 of Rectangle:", area)

SAMPLE INPUT
6
4

OUTPUT
Enter length: 6
Enter width: 4
Area of Rectangle: 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 of Rectangle:", perimeter)

SAMPLE INPUT
6
4

OUTPUT
Enter length: 6
Enter width: 4
Perimeter of Rectangle: 20.0

Program 11: Celsius to Fahrenheit Conversion


CODE
celsius = float(input("Enter temperature in Celsius: "))
fahrenheit = (celsius * 9/5) + 32
print("Temperature in Fahrenheit:", fahrenheit)

SAMPLE INPUT
100

OUTPUT
Enter temperature in Celsius: 100
Temperature in Fahrenheit: 212.0

Program 12: Check Even or Odd


CODE
num = int(input("Enter a number: "))
is_even = (num % 2 == 0)
print("Is the number even?", is_even)

SAMPLE INPUT
7

OUTPUT
Enter a number: 7
Is the number even? False

Program 13: Compare Two Numbers


CODE
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("Is first > second?", a > b)
print("Is first < second?", a < b)
print("Are they equal?", a == b)

SAMPLE INPUT
10
5

OUTPUT
Enter first number: 10
Enter second number: 5
Is first > second? True
Is first < second? False
Are they equal? False

Program 14: Voting Eligibility Check


CODE
age = int(input("Enter your age: "))
eligible = age >= 18
print("Are you eligible to vote?", eligible)

SAMPLE INPUT
20

OUTPUT
Enter your age: 20
Are you 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 of the Circle:", round(area, 2))
SAMPLE INPUT
7

OUTPUT
Enter the radius: 7
Area of the Circle: 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)

SAMPLE INPUT
10
20
30

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 swap: x =", x, ", y =", y)
x, y = y, x
print("After swap: x =", x, ", y =", y)

SAMPLE INPUT
5
9

OUTPUT
Enter first value: 5
Enter second value: 9
Before swap: x = 5 , y = 9
After swap: x = 9 , y = 5

Program 18: Compound Assignment Operators


CODE
num = int(input("Enter a number: "))
print("Original value:", 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)

SAMPLE INPUT
8

OUTPUT
Enter a number: 8
Original value: 8
After += 10: 18
After -= 5: 13
After *= 2: 26
After //= 3: 8

Program 19: Check 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)

SAMPLE INPUT
-7

OUTPUT
Enter a number: -7
Is positive? False
Is negative? True
Is zero? False

Program 20: BMI Calculator


CODE
weight = float(input("Enter your weight in kg: "))
height = float(input("Enter your height in meters: "))
bmi = weight / (height ** 2)
bmi = round(bmi, 2)
print("Your BMI is:", bmi)
print("Is BMI healthy (18.5 - 24.9)?", 18.5 <= bmi <= 24.9)

SAMPLE INPUT
70
1.75

OUTPUT
Enter your weight in kg: 70
Enter your height in meters: 1.75
Your BMI is: 22.86
Is BMI healthy (18.5 - 24.9)? True

You might also like