Department of CSE, PES University
UE24CS151A-Python for Computational Problem Solving
Laboratory Week 4
Problem Statements
1. Write a Python program to Sum only even natural numbers up to n terms given by the
user. Use control flow tokens.
Solution
Explanation
2. Write a Python program to find the factorial of a number using loops. Take input from the
user.
Solution
Explanation
Department of CSE, PES University
UE24CS151A-Python for Computational Problem Solving
Laboratory Week 4
3. Create a Python program that counts down from 10 to 1 using a while loop. For each
number, if the number is odd, print "Odd: <number>", and if it is even, print "Even:
<number>". When it reaches 0, print "Blast off!".
Expected output:
Odd: 9
Even: 8
Odd: 7
Even: 6
Odd: 5
Even: 4
Odd: 3
Even: 2
Odd: 1
Blast off!
Solution
Explanation
Department of CSE, PES University
UE24CS151A-Python for Computational Problem Solving
Laboratory Week 4
4. Write a Python program that takes a number from the user and prints its multiplication
table (from 1 to 10) using a for loop.
Solution
Explanation
5. Create a Python program that prints the numbers from 1 to 30. For multiples of 3, print
"Fizz" instead of the number, and for multiples of 5, print "Buzz". For numbers that are
multiples of both 3 and 5, print "FizzBuzz".
Expected output:
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
Department of CSE, PES University
UE24CS151A-Python for Computational Problem Solving
Laboratory Week 4
FizzBuzz
Solution
Explanation
6. Write a Python program that takes a number as input and checks whether the number is
prime or not. A prime number is a number greater than 1 that has no divisors other than
1 and itself.
Solution
Explanation
Department of CSE, PES University
UE24CS151A-Python for Computational Problem Solving
Laboratory Week 4
7. Write a Python program to print a right-angled triangle of numbers where each row
contains the number repeated multiple times. The number of rows is provided by the
user.
For input 5
1
22
333
4444
55555
Solution
Explanation
8. Write a Python program to find the sum of the digits of a given number. For eg. , for input
123, the output should be 6 (1 + 2 + 3 = 6).
Solution
Department of CSE, PES University
UE24CS151A-Python for Computational Problem Solving
Laboratory Week 4
Explanation
9. John is using a computer in which arithmetic operations (+, -, *, /, **) are not working. But
as his assignments deadline are near he is looking for alternative methods. Help him out
in solving the below questions using other operators.
a. To calculate the power of 2 up to n terms.
Eg. 2 , 2 , …… ,2
0 1 (n-1)
b. Check whether a given number is even or odd.
Hint - express the given num in the binary number system for analysis
and check the least significant bit. (use pen and paper to analyze )
Eg. 5 => 101
10 => 1010
c. Multiple a given number by 33.
Hint - (n * 2 + n)
5
- (33 = 2 + 2 )
5 0
Try using “bitwise or” and “bitwise shifts” operators instead of addition and
multiplication operators.
Solution
Explanation
Department of CSE, PES University
UE24CS151A-Python for Computational Problem Solving
Laboratory Week 4
10. Imagine you are a student calculating your final semester grade. You have 5 subjects,
and each subject has a score between 1 and 100. Write a Python program that:
Takes the score of each subject using a for loop.
Calculates the average score.
Assigns a grade based on the following scale:
o 90 and above: Grade A
o 80-89: Grade B
o 70-79: Grade C
o 60-69: Grade D
o Below 60: Grade F
Prints the average score and the corresponding grade.
Explanation
Department of CSE, PES University
UE24CS151A-Python for Computational Problem Solving
Laboratory Week 4
1. 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
Department of CSE, PES University
UE24CS151A-Python for Computational Problem Solving
Laboratory Week 4
2. 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
Department of CSE, PES University
UE24CS151A-Python for Computational Problem Solving
Laboratory Week 4
Department of CSE, PES University
UE24CS151A-Python for Computational Problem Solving
Laboratory Week 4
3. 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
Explanation
Department of CSE, PES University
UE24CS151A-Python for Computational Problem Solving
Laboratory Week 4
4. 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
Explanation
Department of CSE, PES University
UE24CS151A-Python for Computational Problem Solving
Laboratory Week 4
5. 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
Department of CSE, PES University
UE24CS151A-Python for Computational Problem Solving
Laboratory Week 4
6. Given a 4 digit integer no. , display the individual digit & compute the sum of digits.
Solution
Explanation
Department of CSE, PES University
UE24CS151A-Python for Computational Problem Solving
Laboratory Week 4
7. 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
Department of CSE, PES University
UE24CS151A-Python for Computational Problem Solving
Laboratory Week 4
8. 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
Department of CSE, PES University
UE24CS151A-Python for Computational Problem Solving
Laboratory Week 4
9. 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
Department of CSE, PES University
UE24CS151A-Python for Computational Problem Solving
Laboratory Week 4
10. 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
Department of CSE, PES University
UE24CS151A-Python for Computational Problem Solving
Laboratory Week 4
11. 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
Explanation
Department of CSE, PES University
UE24CS151A-Python for Computational Problem Solving
Laboratory Week 4
12. 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
Explanation