You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#Program to understand working of loops and nested loops in python by a simple pattern formation.
2
+
#suppose if user enters 4 then the following should be printed
3
+
#output -
4
+
# *
5
+
6
+
# * *
7
+
8
+
# * * *
9
+
10
+
# * * * *
11
+
# ignore all the '#' above in the output
12
+
13
+
a=int(input("enter ")) #we take an input from the users of how many lines the pattern should run.
14
+
for i in range(a): #executing a loop, here i's value will start from 0 till a-1.
15
+
t=((a-1)-i) #carefuly observing the pattern we get to understand that we need loop for spaces and one for * so first we define t that will contain values from a-1 to 0
16
+
for b in range(t): #creating a Nested loop for spaces
17
+
print(" ",end=' ')
18
+
k=i+1 #by observing the pattern above we can see that we need a variable that can print the number of * , and by logic the line number = number of * in that line
19
+
for c in range(k): # making a loop to print *
20
+
print("*"," ",end=' ')
21
+
print("\n")
22
+
23
+
# Hence loops work in a defined order to produce a specific result.
24
+
# hence in all , all the logical patterns are solved by nested loop concept and to be a master in that getting the right logic and sufficient number of nested loops is important.
25
+
# If possible try running this python code and try putting different input and see the output
0 commit comments