for Loop
1. Write a python program that displays odd/ even numbers less that 20
for i in range (0,20,2):
print(i,end=', ')
0, 2, 4, 6, 8, 10, 12, 14, 16, 18,
for i in range(1,20,2):
print(i, end= ", ")
1, 3, 5, 7, 9, 11, 13, 15, 17, 19,
print("odd\t\teven")
for i in range(1,20):
print(i+2, "\t\t",i)
odd even
3 1
4 2
5 3
6 4
7 5
8 6
9 7
10 8
11 9
12 10
13 11
14 12
15 13
16 14
17 15
18 16
19 17
20 18
21 19
2. Write a program that computes and displays the sum of odd/even numbers less
than 20
Sum=0
for j in range (1,20,2):
Sum=Sum+j
print("Sum = ",Sum)
Sum = 100
3. Write a program that computes the factorial of a number provided as an input
num= int(input(" Enter a number for the factorial : "))
if num <0:
print(" Please enter a positive number")
elif num==0:
print(" Factorial of zero is 1 ")
else:
fact=1
for i in range (num,1,-1):
fact=fact*i
print("The factorial of {} is {}".format(num,fact) )
Enter a number for the factorial : 98
The factorial of 98 is
9426890448883247745626185743057242473809693764078951663494238777294707
0700232237988829761592077291198236058505886084604294126475673600000000
00000000000000
4. Write a program that computes the HCF of two numbers provided as an input
num1= int(input(" Enter ist number for the HCF : "))
num2= int(input(" Enter 2nd number for HCF:"))
if num1<num2:
smaller=num1
else :
smaller=num2
hcf=1
for i in range (2, smaller+1,1):
if ((num1%i==0) and (num2%i==0)):
hcf=i
print(" HCF is", hcf)
Enter ist number for the HCF : 6
Enter 2nd number for HCF: 7
HCF is 1
4. Problems from the book
total_bugs=0
for i in range(1,6):
bugs= int(input(f"Enter the num of bugs collected each day
{i} :"))
total_bugs += bugs
print("Total num of bugs in 5 days : ", total_bugs)
Enter the num of bugs collected each day 1 : 6
Enter the num of bugs collected each day 2 : 6
Enter the num of bugs collected each day 3 : 6
Enter the num of bugs collected each day 4 : 8
Enter the num of bugs collected each day 5 : 8
Total num of bugs in 5 days : 34
fee= 8000
percent_increase=3
for i in range(1,6):
tution+= (fee*3)/100+fee
print(f"the tution in year {i} :", tution)
the tution in year 1 : 17440.0
the tution in year 2 : 25680.0
the tution in year 3 : 33920.0
the tution in year 4 : 42160.0
the tution in year 5 : 50400.0
while Loop
1. Design a guess game
1. Keep a secret number in a variable
2. which ask the user to guess the number.
3. If the number matches the secret number then congratulate the user
4. otherwise tell the user whether his guess is above or below the secret number.
5. You should ask the user after every turn if he/she wants to continue or not?
6. Also count the number of turns
secret_no= 22
turns=0
while True:
guess= float(input(" Guess the secret num: "))
turns+=1
if guess==secret_no :
print(f"Congratulations, you guessed right in {turns} turns")
break
else:
print("The guess is wrong try again")
Guess the secret num: 33
The guess is wrong try again
Guess the secret num: 22
Congratulations, you guessed right in 2 turns
2. Rewrite the factorial program using while loops
while True:
num = int(input("Enter a number for the factorial: "))
if num < 0:
print("Please enter a positive number")
elif num == 0:
print("Factorial of zero is 1")
else:
fact = 1
i = num
while i > 1:
fact *= i
i -= 1
print(f"The factorial of {num} is {fact}")
Enter a number for the factorial: 3
The factorial of 3 is 6
Enter a number for the factorial: 5
The factorial of 5 is 120
Nested Loops
1. Draw the star pattern
Problems from the book