Aneesh - Sridhar - Week - 4 - Python
Aneesh - Sridhar - Week - 4 - Python
D
UE24CS151A-Python for Computational Problem Solving
Laboratory Week 4
Problem Statements
1. W
rite a Python program to Sum only even natural numbers up to n terms given by the
user. Use control flow tokens.
olution
S
n=int(input("how many terms are to be added"))
sum=0
for i in [0,n+1]:
if i%2!=0:
continue
sum += i
print("even sum = ",sum)
Explanation
)
a he program asks the user for a number (n).
T
b) This number tells how many terms to consider.
c) A variable called sum is set to 0 to keep track of the total of even numbers.
d) The program is supposed to loop through numbers from 0 to n.
e) Inside the loop, it checks if each number is odd.
f) If the number is odd, it skips to the next number.
g) If the number is even, it adds it to sum.
h) Finally, the program shows the total sum of even numbers.
epartment of CSE, PES University
D
UE24CS151A-Python for Computational Problem Solving
Laboratory Week 4
2. W
rite a Python program to find the factorial of a number using loops. Take input from the
user.
olution
S
num=int(input("enter the number whos factorial u want "))
factorial=1
for i in range(1,num+1):
factorial=factorial*i
print("the factorial of ",num, "is ",factorial)
Explanation
)
a he program starts by asking the user to enter a number (num).
T
b) This number is converted from a string to an integer.
c) A variable called factorial is set to 1. This will hold the final result.
d) The program uses a loop to go through numbers from 1 to num.
e) In each iteration, it multiplies the current value of factorial by the loop
variable (i).
f) This continues until all numbers from 1 to num have been multiplied
together.
) Finally, the program prints the factorial of the number entered by the user.
g
epartment of CSE, PES University
D
UE24CS151A-Python for Computational Problem Solving
Laboratory Week 4
3. C reate 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
umber=10
n
while number >= 0:
if number == 0:
print("blast off")
elif number % 2 == 0:
print(f"even ",number)
else:
print(f"odd ",number)
number -= 1
Explanation
) T
a he program starts by setting a variable called number to 10.
b) It enters a while loop that continues as long as number is greater than or
equal to 0.
c) Inside the loop, it first checks if number is 0. If it is, it prints "Blast off!".
d) If number is not 0, it checks if the number is even using the modulus
operator.
e) If the number is even (remainder when divided by 2 is 0), it prints "Even:
<number>".
f) If the number is odd, it prints "Odd: <number>".
g) After checking and printing, the program decreases the number by 1 to
move to the next number in the countdown.
h) The loop repeats until it reaches 0, at which point "Blast off!" is printed.
epartment of CSE, PES University
D
UE24CS151A-Python for Computational Problem Solving
Laboratory Week 4
4. W
rite a Python program that takes a number from the user and prints its multiplication
table (from 1 to 10) using a for loop.
Solution
)
a he program starts by asking the user to enter a number (num).
T
b) This number is converted from a string to an integer.
c) The program uses a for loop to go through numbers from 1 to 10.
d) In each iteration of the loop, it calculates the product of num and the current loop
variable (i).
e) The product is stored in a variable called product.
f ) Then, it prints the multiplication in the format "num x i = product".
g) This process repeats for each number from 1 to 10, generating the full
multiplication table for the entered number.
epartment of CSE, PES University
D
UE24CS151A-Python for Computational Problem Solving
Laboratory Week 4
5. C
reate 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
olution
S
for i in range(1,31):
if i%5 == 0 and i%2 == 0:
print("FizzBuzz")
elif i % 5 == 0:
print("Buzz")
elif i % 3 == 0:
print("Fizz")
else:
print(i)
Explanation
)
a he program uses a for loop to iterate through numbers from 1 to 30.
T
b) For each number (i), it first checks if the number is a multiple of both 3 and 5.
c) If it is, it prints "FizzBuzz".
d) If the number is only a multiple of 5, it prints "Buzz".
e) If the number is only a multiple of 3, it prints "Fizz".
f) If the number is not a multiple of either, it simply prints the number itself.
g) This process continues for all numbers from 1 to 30, generating the desired
output
epartment of CSE, PES University
D
UE24CS151A-Python for Computational Problem Solving
Laboratory Week 4
6. W
rite 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
um=int(input("enter the number to check if prime or not "))
n
for i in range(2,num):
if num%i==0:
print("number is not a prime number")
else:
rint("number is a prime number")
p
xplanation
E
) T
a he program starts by asking the user to enter a number (num).
b) This number is converted from a string to an integer.
c) The program uses a for loop to check each number (i) starting from 2 up to one
less than num.
d) Inside the loop, it checks if num is divisible by i using the modulus operator.
e) If num is divisible by any i, it means num is not a prime number, so it prints
"number is not a prime number".
f) However, if num is not divisible by any number in the loop, the program
incorrectly continues to print "number is a prime number" for each iteration of the
loop.
g) This logic should be modified to only print "number is a prime number" after
checking all possible divisors.
h) After the loop, if no divisors are found, the program should confirm that the
number is prime.
epartment of CSE, PES University
D
UE24CS151A-Python for Computational Problem Solving
Laboratory Week 4
7. W
rite 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
r ows=int(input("enter no of rows"))
for i in range (1,rows+1):
print(str(i)*i)
Explanation
) T
a he program starts by asking the user to enter the number of rows (rows).
b) This number is converted from a string to an integer.
c) The program uses a for loop to iterate through numbers from 1 to the number of
rows.
d) In each iteration (i), it prints the string representation of the current number (i)
repeated i times.
e) This creates a new line for each row, forming a right-angled triangle pattern.
f) The output consists of rows of numbers where each row corresponds to the row
number, with the number repeated according to the row's index.
epartment of CSE, PES University
D
UE24CS151A-Python for Computational Problem Solving
Laboratory Week 4
8. W
rite 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).
olution
S
num=int(input("enter the number: "))
sum = 0
while num != 0:
sum += num%10
num//=10
print("sum = ",sum)
Explanation
)
a he program starts by asking the user to enter a number (num).
T
b) This number is converted from a string to an integer.
c) A variable called sum is initialized to 0 to keep track of the total sum of the digits.
d) The program uses a while loop that continues as long as num is not equal to 0.
e) Inside the loop, the program adds the last digit of num to the sum using the modulus
operator (num % 10).
f) It then removes the last digit from num by performing integer division by 10 (num //= 10).
g) This process repeats until all digits have been added to the sum.
h) Finally, the program prints the total sum of the digits.
epartment of CSE, PES University
D
UE24CS151A-Python for Computational Problem Solving
Laboratory Week 4
9. J
ohn 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
a) n
= int(input("enter the number"))
for i in range(n):
print(f"2^{i} = {1 << i}")
b) n
um = int(input("enter the number: "))
if num & 1 == 0:
print("Even")
else:
print("Odd")
c) n
= int(input("enter the number: "))
result = (n << 5) | n #(|) is th or operator
print(result)
epartment of CSE, PES University
D
UE24CS151A-Python for Computational Problem Solving
Laboratory Week 4
Explanation
a) The program starts by asking the user to enter a number n. This number represents how many powers of 2 will be printed.
Inside the loop, it calculates 2 raised to the power of i using the bitwise left shift operation.
The expression 1 << i shifts the number 1 to the left by i positions. This operation effectively calculates 2 raised to the power of i.
For example, if i is 3, then 1 << 3 results in 8, which is 2 raised to the power of 3.
The program prints the result in the format "2 raised to the power of i = result" using an f-string for better readability.
The output will show the powers of 2 from 2 raised to the power of 0 to 2 raised to the power of n-1.
b) The program starts by asking the user to enter a number (num).
The program uses the bitwise AND operator to check if the last bit of the number is 0.
If the expression equals 0, it means the number is even, so it prints "Even".
If the expression is not 0, it means the number is odd, so it prints "Odd".
This program effectively determines the parity of the number using bit manipulation.
c) The program starts by asking the user to enter a number (n).
Then, it combines this shifted value with n using the bitwise OR operator.
he expression results in a new number that has the original number n in the least significant bits and the shifted value in the more
T
significant bits.
0. Imagine you are a student calculating your final semester grade. You have 5 subjects, and
1
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
Solution
total_score = 0
number_of_subjects = 5
total_score += score
Explanation
● T he program initializes a variable called total_score to 0 to keep track of the total points
across all subjects.
● It sets the variable number_of_subjects to 5, representing the number of subjects.
● The program uses a for loop to iterate over each subject from 1 to 5.
● Inside the loop, it prompts the user to enter the score for each subject, ensuring the
score is a float.
● A while loop checks if the entered score is valid (between 1 and 100). If the score is
invalid, it prompts the user to enter the score again.
● The valid score is then added to total_score.
● After collecting scores for all subjects, the program calculates the average score by
dividing total_score by number_of_subjects.
● It then uses a series of if-elif statements to determine the grade based on the average
score.
.
1 for an average score of 90 or above
A
2. B for scores between 80 and 89
3. C for scores between 70 and 79
4. D for scores between 60 and 69
5. F for scores below 60