0% found this document useful (0 votes)
13 views

Lab 2 Practice Problems

Uploaded by

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

Lab 2 Practice Problems

Uploaded by

yashfa ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Write Python Code for the following Problems

● Write a program that takes the radius of a circle as input from the user and calculates the
area of the circle. (Formula: area = π * r^2, assume π = 3.14)
● Write a program that asks the user for the prices of three grocery items and calculates
the total bill.
● Write a program that takes the length and width of a rectangle as input and calculates
the perimeter. (Formula: perimeter = 2 * (length + width))
● Write a program that takes an input number of minutes and converts it into hours and
minutes. For example, 130 minutes is 2 hours and 10 minutes.
● Write a program that takes the user’s weight in kilograms and height in meters and
calculates their BMI. (Formula: BMI = weight / (height^2))

For the following codes, try to think about the output of the code without
executing it and then try executing it to see if your answer was correct

Code 1:

price1 = 10
price2 = 5
total = price1 + price2
print("Total Price:", total)

Code 2:

length = 12
width = 4
perimeter = 2 * (length + width)
print("Perimeter:", perimeter)

Code 3:

hours = 2
minutes = hours * 60 + 30
print("Total Minutes:", minutes)

Code 4:

weight = 70
height = 1.75
bmi = weight / (height ** 2)
print("BMI:", bmi)

Code 5:

radius = 5
pi = 3.14
area = pi * radius ** 2
print("Area of Circle:", area)

Following code snippets has some missing lines, try to read comments to
understand how the code is supposed to work and complete the code by
adding missing code.

Code 1:

# Program to calculate the perimeter of a square


side = float(input("Enter the side length of the square: "))
# Missing code to calculate and display the perimeter (Formula: perimeter = 4 * side)

Code 2:

# Program to calculate the average of three numbers


num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
num3 = int(input("Enter third number: "))
# Missing code to calculate the average and display it

Code 3:

# Program to calculate the area of a trapezoid


# Missing input part (base1, base2, and height)
# Missing code to calculate and display the area (Formula: area = 0.5 * (base1 + base2) *
height)

Code 4:
# Program to calculate the discounted price of an item
original_price = float(input("Enter the original price: "))
discount_percent = float(input("Enter the discount percentage: "))
# Missing code to calculate and display the discounted price (Formula: discounted_price =
original_price - (original_price * discount_percent / 100))

Code 5:

# Program to determine if the transaction is a profit or loss


cost_price = float(input("Enter the cost price: "))
selling_price = float(input("Enter the selling price: "))
# Missing code to calculate and display whether it's a profit or a loss and by how much

You might also like