Aneesh Sridhar Week3 Python
Aneesh Sridhar Week3 Python
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
Explanation
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
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
Explanation
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
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?
Solution
Explanation
The users are said to enter the total bill amount and the
number of friends who have contributed to the bill as the
input.
Now the computer takes the input and performs simple
division ( total bill / total people )
This provides a number that is equal to the amount to be
paid by every individual or friend.
This amount is displayed as output due to the print
function.
Department of CSE, PES University
UE24CS151A-Python for Computational Problem Solving
Laboratory Week 3
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
The user is first asked to enter the amount of work to be done and
the number of people available to do the work as input.
The computer uses two new operators are used: mod(%) and
double slash(//)
It use double slash to determine work for each person and mod
for the amount of work left.
The double slash only produces the quotient of the solved
equation, while the mod function only produces the remainder.
After the inputs are received the computer does simple division to
get quotient and remainder using above operators.
The answer is now display as output using the print function.
Department of CSE, PES University
UE24CS151A-Python for Computational Problem Solving
Laboratory Week 3
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.
Input:
Solution
tt = 0
if af%at == 0:
tt = af // at
else:
tt = (af // at) + 1
tc = tt*p*af*c
Explanation
The user is asked to enter the dimension of the floor ( length and breadth ), the length of the square
tile, price of each tile and labor cost per unit area of tile.
The area of floor is calculated using inputs = length * breadth.(af)
The area of tile is calculated = length * length(at)
Total tiles required(tt) is taken as zero then the if function is used.
If af/at gives no reminder or af%at is 0 then the exact number of tiles needed is given and the total
tiles required value is updated from 0 to (af//at).
Else, that is af%at is not zero then the number of tiles required becomes (af//at) + 1.
The computer now has all inputs including number of tiles required.
To calculate total cost it does simple multiplication of total tiles * price of one tile * area of floor *
labor cost.
The answers obtained for total tiles and total cost are displayed as output due to print function.
Department of CSE, PES University
UE24CS151A-Python for Computational Problem Solving
Laboratory Week 3
8. Given a 4 digit integer no. , display the individual digit & compute the sum of digits.
Solution
Explanation
The user is asked to enter a 4 digit number (a)
This numbers every digit is named as d1 d2 d3 and d4.
D1 is found by : a%10
Now after every step we get new number which is labelled a1 and a2 in this case.
Thus the following formulas are applied to find the following values:
A1=a//10
A2=a1//10
D1=a%10
D2=a1%10
D3=a2%10
D4=a2//10
The problem to solve is sum of all digits of the number, that is sum of d1, d2, d3
and d4.
Since the computer already has these values it performs simple addition to find the
sum.
The sum is displayed as the output due to the print function.
The digits of the entered number are also displayed as output separately due to
the print function.
Department of CSE, PES University
UE24CS151A-Python for Computational Problem Solving
Laboratory Week 3
9. 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
Explanation