Roll No.27
Roll No.27
Roll No.27
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
# Function to calculate sum of even natural numbers up to n terms
def sum_even_numbers(n):
total_sum = 0
count = 0 # To track how many even numbers have been added
number = 2 # Start from the first even number
return total_sum
result = sum_even_numbers(n_terms)
print(f"The sum of the first {n_terms} even natural numbers is: {result}")
2. Write a Python program to find the factorial of a number using loops. Take input from the
user.
Solution
# Function to calculate factorial using a loop
def factorial(n):
result = 1
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!
Department of CSE, PES University
UE24CS151A-Python for Computational Problem Solving
Laboratory Week 4
Solution
# Countdown function
def countdown():
number = 10
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
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
FizzBuzz
Solution
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
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
Department of CSE, PES University
UE24CS151A-Python for Computational Problem Solving
Laboratory Week 4
55555
Solution
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
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)
- (33 = 2 + 2 )
5 0
Try using “bitwise or” and “bitwise shifts” operators instead of addition and
multiplication operators.
Solution
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
Department of CSE, PES University
UE24CS151A-Python for Computational Problem Solving
Laboratory Week 4
Extra Questions
1. 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
# Given masses
mass_D = 1 # amu
mass_E = 5 # amu
2. 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
import math
def calculate_colors_area_perimeter(radius):
# Area of the quarter circle
area_quarter_circle = (1/4) * math.pi * radius**2
# Perimeter calculations
perimeter_red = (1/4) * (2 * math.pi * radius) + 2 * radius
perimeter_blue = radius / 3 # Straight boundary of blue
perimeter_yellow = radius / 3 # Straight boundary of yellow
return {
'area_red': area_red,
'area_blue': area_blue,
'area_yellow': area_yellow,
'perimeter_red': perimeter_red,
'perimeter_blue': perimeter_blue,
'perimeter_yellow': perimeter_yellow,
}
Department of CSE, PES University
UE24CS151A-Python for Computational Problem Solving
Laboratory Week 4
3. 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
def celsius_to_fahrenheit(celsius):
"""Convert Celsius to Fahrenheit."""
fahrenheit = celsius * (9/5) + 32
return fahrenheit
def main():
# Prompt user for temperature in Celsius
try:
celsius = float(input("Enter the temperature in Celsius: "))
fahrenheit = celsius_to_fahrenheit(celsius)
print(f"{celsius}°C is equivalent to {fahrenheit:.2f}°F")
except ValueError:
print("Please enter a valid number.")
4. 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
def main():
# Prompt user for the number of servings they want to make
try:
desired_servings = int(input("Enter the number of servings you want to make: "))
5. 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
def calculate_slices(pizzas, slices_per_pizza, attendees):
"""Calculate the number of slices each person gets and the leftover slices."""
total_slices = pizzas * slices_per_pizza
slices_per_person = total_slices // attendees # Integer division for slices per person
leftover_slices = total_slices % attendees # Modulus for leftover slices
return slices_per_person, leftover_slices
def main():
try:
# Input for the number of pizzas, slices per pizza, and number of attendees
pizzas = int(input("Enter the number of pizzas: "))
slices_per_pizza = int(input("Enter the number of slices per pizza: "))
attendees = int(input("Enter the number of people attending: "))
# Display results
print(f"Each person gets {slices_per_person} slices of pizza.")
print(f"There will be {leftover_slices} leftover slices.")
except ValueError:
print("Please enter valid numbers.")