Looping Programs
1.
Write
a program to display all the numbers from 0 to 5.
for
x in range(0,6):
print(x)
Output:
0
1
2
3
4
5
2.
Write
a program to display all the numbers from 1 to 50.
for
x in range(1,51):
print (x, end =" ")
Output:1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
3.
Write
a program to display 1 3 5 7 9
for
i in range(1,10,2):
print(i, end=" ")
4.
Write
a program to display 2 4 6 8 10
for
i in range(2,11,2):
print(i, end=" ")
5.
Write
a program to display all the even numbers from 2 to 100.
for
i in range(2,101,2)
print(i, end=" ")
output:
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56
58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100
6.
Write
a program to display all the numbers from 1 to 50 using while loop.
a
= 1
while
a <= 50:
print(a, end=" ")
a = a + 1
Output:1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
7.
Write
a program to display all the numbers from 50 to 1.
for
n in range (50,0,-1):
print (n, end = " ")
Output:
50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25
24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
8.
Write
a program to display 1 4 9 16...........up to 10th terms
for
i in range(1,11):
n
=i* i
print(n,
end=" ")
OR
for
i in range(1,11):
n =i**2
print(n, end=" ")
output:
1 4 9 16 25 36 49 64 81 100
9.
Write
a program to display 1 5 9 13.........up to 10th terms
x
= 1
n
= 1
while
x <= 10 :
print(n, end= " ")
n = n + 4
x = x + 1
Output:1
5 9 13 17 21 25 29 33 37
10.
Write
a program to display
5 55 555 5555 55555
n=5
for
x in range(1,6):
print(n, end= " ")
n=n*10+5
11.
Write
a program to display
5
55
555
5555
55555
n=5
for
x in range(1,6):
print(n)
n=n*10+5
12.
.100,97,94...........up
to 20th terms
N=100
for
y in range(1,21):
print(n, end=" ")
n =n - 3
13.
Write
a program to display the sum of all the numbers from 1 to 20.
s
= 0
z=1
while
z<=20:
s=s+z
z =z+1
print("sum
of all the numbers from 1 to 20 is”,s)
Output:
sum of all the numbers from 1 to 20 is 210
14.
Write
a program to display the factorial of the given number
p
= 1
n=
int(input("Enter a number: "))
for
i in range(1,n+1):
p =p * i
print(f"The
factorial of {n} is {p}")
Output:Enter
a number: 3
The
factorial of 3 is 6
15.
Write
a program to find the factors of the given number.
n=int(input("Enter
the number"))
print("Factors
are")
for
i in range(1,n+1):
if n%i==0:
print(i)
Output:
Enter the number 6
Factors
are
1
2
3
6
16.
Write
a program to display the multiplication table of the given number.
n
= int(input("Enter the number: "))
for
i in range(1, 11):
p = n * i
print(f"{n} x {i} = {p}")
Output:
Enter
the number: 2
2
x 1 = 2
2
x 2 = 4
2
x 3 = 6
2
x 4 = 8
2
x 5 = 10
2
x 6 = 12
2
x 7 = 14
2
x 8 = 16
2
x 9 = 18
2
x 10 = 20
17.
Write
a program to display whether the given number is prime or composite.
n
= int(input("Enter the number: "))
c
= 0
for
i in range(1, n + 1):
if n % i == 0:
c = c + 1
if
c == 2:
print("The number is prime")
else:
print("The number is composite")
Output:
Enter the number: 4
The
number is composite
18.
Write
a program to display the given number in reverse order.
n
= int(input("Enter a number: "))
s
= 0
while
n != 0:
r = n % 10
s = s * 10 + r
n = n // 10
print("Reversed
number is:",s)
Output:
Enter a number: 987
Reversed
number is: 789
19.
Write
a program to display whether the given number is palindrome or not.
n
= int(input("Enter a number: "))
temp
= n
s
= 0
while
n != 0:
r = n % 10
s = s * 10 + r
n = n // 10
if
temp == s:
print("The number is a
palindrome")
else:
print("The number is not a
palindrome")
Output:Enter
a number: 343
The
number is a palindrome
20.
Write
a program to display the multiplication table of numbers from 2 to 4
for
i in range(2, 5):
for j in range(1, 11):
n = i * j
print(f"{i} x {j} = {n}")
print()
# Blank line after each table
21.
Write
a program to display the sum of individual digits of the given number
n
= int(input("Enter a number: "))
s
= 0
while
n != 0:
r = n % 10
s = s + r
n = n // 10
print("Sum
of individual digits is:", s)
Output:
Enter a number: 23
Sum
of individual digits is: 5
22.
Write
a program to display the number in the following pattern.
12345
1234
123
12
1
for
i in range(5, 0, -1):
for j in range(1, i + 1):
print(j, end="")
print()
# Move to next line after inner loop
23.
Write
a program to display whether the given number is armstrong or not.(153,370,371,407,1634
etc)
n
= int(input("Enter a number: "))
temp
= n
s
= 0
while
n != 0:
r = n % 10
s = s + r**3
n = n // 10
if
temp == s:
print("The number is an Armstrong
number")
else:
print("The number is not an Armstrong
number")
24.
Write
a program to display the Fibonacci series.(2,1,3,4,7………….upto 10th
term)
a
= 2
b
= 1
terms=
10
print("Fibonacci
series:", end=" ")
for
i in range(terms):
print(a, end=" ")
c = a + b
a = b
b = c
25.
Write
a program to display the Hailstone series.(7,22,11,34………… upto 10th
term)
n = 7
terms= 10
print("Hailstone sequence:")
for i in range(terms):
print(n, end=" ")
if n % 2 == 0:
n = n // 2
else:
n = 3 * n + 1