Practical 4: Generate the following patterns using nested loop

Pattern to produce

12345

1234

123

12

1

Program code

 
j=6
for i in range(5):
  for j in range(1,j):
    print(j,end=" ")
  print('\n')
 

Explanation:- In this program we want to produce the above shown pattern on the screen. This program teaches us about execution of loop constructs in the programming. We have 2 for loops in this programs, nested one into other. loop means repetition of task.

In programming innermost loop completes its task code first, then next outer loop completes its execution task code(lines of code) and so on till outermost loop from where we have entered the nested loops completes its execution. In above program we have used range function.

Outer For loop executes first time,Then flow enters inner loop where loop will execute its code 5 times(range 1 to 6(excluding 6)). end=” “ means end printing after printing space. It will not go to next line. So print function will print 1 to 5 (print will execute 5 times in inner loop).

Then in next line outer loop executes 2nd time and again enters inner loop , where j is printed upto 4 with spaces in between, and program continues until outer loop complete 5 rounds of execution this way.