Pythonprograms Assingment1
Pythonprograms Assingment1
Problem Statements
1. Input the base (b) and height (h) of a triangle and calculate its area where
Area of triangle =0.5* b * h
Solution
b=float(input("enter base"))
h=float(input("enter height"))
area=0.5*b*h
print("area is", area)
Explanation
This code calculates the area of a triangle using the formula: Area = 0.5 * base * height.
1. The code first prompts the user to input the base and height of the triangle using
float(input()), which converts the input into a floating-point number.
2. The inputs are stored in variables b (base) and h (height).
3. The area is calculated using the formula 0.5 * b * h and stored in the variable area.
4. Finally, the code prints the calculated area to the console using print("area is", area).
Department of CSE, PES University
UE24CS151A-Python for Computational Problem Solving
Laboratory Week3
2. You are traveling to Kenya, if 1 Kenyan shilling = 0.65 Indian rupee. Calculate how many
shillings you will have based on the input in Indian Rupees.
Solution
Explanation
This code converts Indian Rupees (INR) to Kenyan shilling based on a fixed exchange
rate.
3. A grocery store offers a 10% discount on all purchases. Write a program that asks the
user for the total amount of their purchase, applies the discount and prints the total.
Solution
a=float(input("amount"))
b=a-(a*0.1)
print("amount to bepaid",b)
Explanation
This code calculates the amount to be paid after applying a 10% discount.
Example output:
Amount: 100
Amount to be paid: 90.0
Department of CSE, PES University
UE24CS151A-Python for Computational Problem Solving
Laboratory Week3
4. You need to introduce yourself to your new classmates. What will you say as an ice
breaker? Display it in the format: My name is _____ and a fun fact about me is ____
Solution
Explanation
This code collects and displays a user's name and a fun fact about themselves.
1. The user is prompted to input their name and a fun fact, stored in variables a and b
respectively.
2. The code then prints out a personalized message combining the input name and fun
fact.
5. You go out for dinner with your friends and need to split the bill equally, how will you do so
by taking input of the bill amount and number of friends?
Department of CSE, PES University
UE24CS151A-Python for Computational Problem Solving
Laboratory Week3
Solution
Explanation
This code calculates and displays the amount each person needs to pay when splitting a bill
evenly among friends.
1. The user inputs the total bill amount (a) and the number of friends (b).
2. The code divides the bill amount by the number of friends (c = a/b) to calculate the
individual share.
3. The result is printed, showing the amount each friend needs to pay.
Department of CSE, PES University
Laboratory Week3
Department of CSE, PES University
UE24CS151A-Python for Computational Problem Solving
LaboratoryWeek3
6. You have a certain amount of total work(n) that needs to be shared among a fixed number
of workers m. Write a program to calculate the amount of work for each worker and how
much extra work is left. Take input for no. of work and no. of workers.
eg: n =
100 m
=6
Solution
Explanation:
This code divides a total amount of work among a specified number of workers and
calculates any remaining work.
1. The user inputs the total work (a) and the number of workers (b).
2. The code divides the total work by the number of workers (c = a/b) to determine each
worker's share.
3. The remainder (y = a%b) calculates any work left over after dividing.
7. Tiling of a rectangular floor is performed. Write a program that calculates the number
of tiles required and the total cost to tile the entire floor. Take the dimensions of the floor
and the size and price of each tile as input from the user. Also consider labor charges.
Department of CSE, PES University
UE24CS151A-Python for Computational Problem Solving
LaboratoryWeek3
Input:
Solution
area_floor=l_f*b_f
area_tile=a*a
total_tiles=0
if area_floor%area_tile==0:
total_tiles=area_floor//area_tile+1
total_cost=total_tiles*p_pt+area_floor*l_cost
print("total tiles:",total_tiles) print("total
cost:",total_cost)
Explanation
This code calculates the number of tiles needed and the total cost for tiling a floor. It
prompts the user to input the floor's dimensions, tile size, tile price, and labor cost per
unit area. It calculates the total floor area and the area covered by a single tile, then
determines the number of tiles needed. Finally, it computes the total cost, which includes
the cost of tiles and labor, and prints both the total number of tiles and the overall cost.
Department of CSE, PES University
UE24CS151A-Python for Computational Problem Solving
LaboratoryWeek3
8. Two persons A and B are conducting a Chemical experiment. In which A is user element
D and E in ratio of 2:1 and B uses the ratio of 4:3, calculate the mass of the compound if
each generates a 1 mole gram of substance given that the mass of D is 1 unit(amu) and
E is 5 units(amu). Take the values from the user. Hint - Consider using split() method on
the input string eg-
"1:2".split(":") # [‘1’,’2’] a,b = "1:2".split(":") # a <- '1' ;
b <- ‘2 a,b = int(a), int(b) # type casting char/string
to integers
Solution
Explanation
9. Given a 4 digit integer no. , display the individual digit & compute the sum of digits.
Solution
This code extracts and sums the digits of a four-digit number input by the user. It
calculates each digit by using division and modulus operations: `a` for the thousands, `b`
for the hundreds, `c` for the tens, and `d` for the units. The digits are printed separately,
followed by their sum. Finally, it outputs the total sum of the digits.
Department of CSE, PES University
UE24CS151A-Python for Computational Problem Solving
LaboratoryWeek3
10. You are asked to color quarter of a circle withreed, one third of the remaining with blue
and rest in yellow, find the area covered by each color and perimeter of each color
(include inner boundaries also ). Take radius as input.
Solution
Explanation
11. You've just celebrated your birthday, and you're curious about how many months, days,
and hours you've been alive. Write a Python program that takes your age(in years) as
input and prints your age in months and days.
Solution
print("month", months)
print("days", days) print("hours",
hours)
Explanation
Department of CSE, PES University
UE24CS151A-Python for Computational Problem Solving
LaboratoryWeek3
This code calculates and displays a person's age in months, days, and hours based on
their inputted age in years. It multiplies the age by 12 to find the number of months, 365
for the number of days, and 365 multiplied by 24 for the number of hours. Each
calculated value is then printed to the console. The code provides a simple way to
convert years into different time units.
12. Imagine you've just opened a savings account at your local bank. You're curious how
much interest you'll earn after a few years. Write a Python program that calculates the
simple interest based on the amount you've deposited, the bank’s interest rate, and the
number of years you plan to keep the money in the account.
Solution
Explanation
This code calculates the simple interest based on user inputs for principal amount, time
period, and interest rate. It prompts the user to enter these values as floating-point
numbers. The simple interest is calculated using the formula simple interest=ptr/100.
Finally, it prints the calculated simple interest to the console.
13. You’re planning a vacation to a different country, but their weather forecasts are in
Celsius, and you’re more familiar with Fahrenheit. Write a Python program to help you
convert temperatures from Celsius to Fahrenheit so you can pack accordingly for your trip!
(use the formula: F = C * 9/5 + 32)
Solution
Explanation
14. You’re baking a cake, but the recipe you’re following is for 12 servings, and you need to
make enough for a different number of people. Write a Python program that takes the
number of servings you want to make and adjusts the ingredients accordingly. If the
recipe calls for 2 cups of sugar for 12 servings, calculate how many cups of sugar you'll
need for the number of servings entered by the user.
Solution
num_servings=int(input('NUMBER OF SERVINGS='))
n= num_servings/6
print("cups of sugar needed is", n)
Department of CSE, PES University
UE24CS151A-Python for Computational Problem Solving
LaboratoryWeek3
Explanation
This code calculates the amount of sugar needed based on the number of servings
provided by the user. It prompts the user to input the number of servings as an integer.
We know that that every 6 servings require 1 cup of sugar, so it divides the number of
servings by 6 to find the cups needed. Finally, it prints the calculated amount of sugar
required.
15. You're throwing a pizza party for your friends, and you want to make sure everyone gets
an equal number of slices. Write a Python program that takes the number of pizzas,
slices per pizza, and the number of people attending the party as input, and calculates
how many slices each person gets. Also, calculate how many slices will be left over.
Solution
ans= (a*b)//c
rem= (a*b)%c
print("slices per person=", ans, "remaining slices =", rem)
Department of CSE, PES University
Explanation
This code calculates how many pizza slices each person at a party will receive and how
many slices will remain. It tells the user to input the number of pizzas, slices per pizza,
and the number of people attending. The total number of slices is divided by the number
of people to determine the slices per person, while the remainder gives the leftover
slices. it prints both the number of slices per person and the remaining slices.