Xi-Cs-Nested Loop in Python
Xi-Cs-Nested Loop in Python
Nested loop
When one loop is placed inside the body of another loop, then the structure is
called Nested loop. i.e. one loop inside another loop is called Nested loop.
For example:
Note:-
• In case nested loop the inner loop is always executed for each and every value of
outer loop.
• When the outer loop is terminated then only the Nested loop structure is
terminated.
Nested loop Programs
Program 1: To print the following pattern with the help of the
concept of Nested loop
*
for i in range(1,5): # No. of rows
**
for j in range(1,i+1): ***
****
print(“*”,end=“
”)
print()
Nested loop Programs
Program 2: To print the following pattern with the help of the
concept of Nested loop
1
for i in range(1,5): 22
333
for j in range(1,i+1): 4444
print(i ,end=“ ”)
print()
Nested loop Programs
Program 3: To print the following pattern with the help of the
concept of Nested loop
1
for i in range(1,5):
12
for j in range(1,i+1): 123
1234
print(j ,end=“
”)
print()
Nested loop Programs
Program 4: To print the following pattern with the help of the
concept of Nested loop
2
for i in range(1,5):
34
for j in range(1,i+1): 456
5678
print(i+j , end=" ")
print()
OR
p=2
for i in range(1,5): 2
c=p 34
for j in range(1,i+1): 456
print(c , end=" ") 5678
c=c+1
p=p+1
print()
Nested loop Programs
Program 5: To print the following pattern with the help of the
concept of Nested loop
1
for i in range(1,5):
13
p=1 135
1357
for j in range(1,i+1):
print(p, end=" ")
p = p+2
print()
Nested loop Programs
Program 6: To print the following pattern with the help of the
concept of Nested loop
OR
p=4
for i in range(1,5):
for j in range(5,i,-1):
print(p, end=" ")
print()
p=p-1
Nested loop Programs
Program 7: To print the following pattern with the help of the
concept of Nested loop
for i in range(5,0,-1): 5
for j in range(i,6): 44
print(i,end=" ") 333
print() 2222
11111
OR
p=5
for i in range(1,6):
for j in range(1,i+1):
print(p,end=" ")
print()
p=p-1
Nested loop Programs
Program 8: To print the following pattern with the help of the
concept of Nested loop
for i in range(5,0,-1): 5
for j in range(5,i-1,-1): 54
print(j,end=" ") 543
print() 5432
54321
OR
for i in range(1,6):
p=5
for j in range(1,i+1):
print(p,end=" ")
p=p-1
print()
Nested loop Programs
Program 9: To print the following pattern with the help of the
concept of Nested loop
for i in range(1,5): A
p=65 AB
for j in range(1,i+1): ABC
print(chr(p),end=" ") ABCD
p=p+1
print()
A
BC
CDE
Note: DEFG
A-Z = 65-90
a-z = 97-122
Nested loop Programs
Program 10: WAP to find the sum of the following series upto nth term
entered by the user.
(1)+(1+2)+(1+2+3)+(1+2+3+4)+........(1+2+3+.....n)
s = 0
n = int(input(“ Enter the last term”))
for i in range(1,n+1):
for j in range(1,i+1):
s = s+j
print(“Sum of the series=“,s)
Nested loop Programs
Program 11: WAP to find the sum of the following series upto nth term
entered by the user.
12 + (12+22) + (12+22+32) + (12+2232+42) + (12+2232+42+……………n2 )
s = 0
n = int(input(“ Enter the last term”))
for i in range(1,n+1):
for j in range(1,i+1):
s = s+j*j
print(“Sum of the series=“,s)
Nested loop Programs
Program 12: WAP to find the sum of the following series upto nth term
entered by the user.
1! + 2! + 3! + 4! + ………n!
n = int(input("Enter any number"))
s = 0
for i in range(1,n+1):
f=1
for j in range(i,0,-1):
f=f*j
s = s+f
print("Sum of factorial series=", s)
Thank You