0% found this document useful (0 votes)
16 views9 pages

Aneesh Sridhar Week3 Python

python

Uploaded by

aruukadlabal
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)
16 views9 pages

Aneesh Sridhar Week3 Python

python

Uploaded by

aruukadlabal
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/ 9

Department of CSE, PES University

UE24CS151A-Python for Computational Problem Solving


Laboratory Week 3

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

base = float(input("Enter the base of the triangle: "))


height = float(input("Enter the height of the triangle: "))
area = 0.5*base*height
print("The area of the triangle is",area)

Explanation

 User enters measurements for base and height of triangle


 The computer is provided with the formula to use when user
enter the numerical values for base and height of the triangle.
Formula is 0.5*base*height.
 The computer processes the info given to produce answer
 This answer will be displayed as output due to the print
function.
Department of CSE, PES University
UE24CS151A-Python for Computational Problem Solving
Laboratory Week 3

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

a = float(input("enter the amount in rupees: "))


k = 0.65*a
print("The amount in kenya shilling is: ",k)

Explanation

 User is asked to enter the amount of Indian rupees he


has as input.
 1 kenya shilling is equal to 0.65 indian rupee so the
formula (shilling = 0.65 * rupee) is used.
 Using the input and the formula the computer
processes the answer
 It produces the answer and output using print function
Department of CSE, PES University
UE24CS151A-Python for Computational Problem Solving
Laboratory Week 3

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

b = float(input("Enter the total amount of purchase: "))


disc = 0.1*b
print("The discount on your purchase is: ",disc)

Explanation

 The user is asked to enter the total bill amount as input


to process the discount.
 The discount to be applied is 10% which is total
amount * 0.1.
 The formula used is (total amount * 0.1)
 By using the formula and input the computer processes
the answer.
 The answer is displayed as output using print function.
Department of CSE, PES University
UE24CS151A-Python for Computational Problem Solving
Laboratory Week 3

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

a = input("Enter your name: ")


b = input("a fun fact about you: ")
print("My name is", a, "and a fun fact about me is", b)

Explanation

 The user is asked to enter his name and a fun


fact about himself as input.
 These inputs are taken and are joined with
predefined text sentences like "My name is"
 And "and a fun fact about me is" .
 This final sentence is displayed as output using
print function
Department of CSE, PES University
UE24CS151A-Python for Computational Problem Solving
Laboratory Week 3

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

a = float(input("Enter total amount in bill: "))


b = int(input("enter no. of friends: "))
s = a/b
print("Each person pays: ",s)

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

work on each worker = 16


extra work left = 4

Solution

n = float(input("enter total amount of work: "))


m = int(input("enter total no. of workers: "))
wd = n//m
wl = n%m
print("total work dine by each worker is: ",wd)
print("The remaining work is: ",wl)

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:

Length of the floor (l_f - float)


Width of the floor (w_f - float)
Size of each tile (a - float) - assume it is a square tail
Cost per tile (p_pt - float)
labor charge per unit area (l_cost - float
Output:
Maximum number of tiles required (int)
Total cost of tiling the floor (float)

Solution

l = float(input("Enter length of floor: "))


b = float(input("enter breadth of floor: "))
s = float(input("enter side of tile: "))
p = float(input("price of each tile:" ))
c = float(input("enter the labour charges(per unit area): "))
af = l*b
at = s**2

tt = 0
if af%at == 0:
tt = af // at
else:
tt = (af // at) + 1

tc = tt*p*af*c

print("total tiles are: ",tt)


print("total cost is: ",tc)

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

a = int(input("Enter a 4 digit no."))


d1 = num%10
a1 = num//10
d2=a1%10
a2=a1//10
d3=a2%10
d4=a2//10
print(f"1st digit: {d4},\n2nd digit: {d3}\n3rd digit:{d2}\n4th
digit:{d1}")
print(f"The sum of {a} is {d1+d2+d3+d4})

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

age = int(input("enter your age in years: "))


months = age*12
days = age *365
hrs = age*365*24

print(f"you are {months} months and {days} days and {hrs}


hrs old")

Explanation

 The user is asked to enter his age as the input.


 The input is taken by the computer to process for the following data:
For number of months the input is multiplied with 12. ( age * 12 )
For number of days the input is multiplied with 365. ( age * 365 )
For number of hours the input is multiplied with 365 and then
again with 24. ( age * 365 * 24 )
 Since the computer has all the required answers, it combines the
answer in the pre set sentence:
 (f"you are {months} months and {days} days and {hrs} hrs old").
 This sentence is then displayed as output due to the print function.

You might also like