Q:

Write a python program to use for loop to print the following reverse number pattern

0

Print the following pattern

Write a program to use for loop to print the following reverse number pattern

5 4 3 2 1 
4 3 2 1 
3 2 1 
2 1 
1

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Hint:

  • Set row = 5 because the above pattern contains five rows
  • create an outer loop to iterate numbers from 1 to 5 using for loop and range() function
  • Create an inner loop inside the outer loop in such a way that in each iteration of the outer loop, the inner loop iteration will be reduced by ii is the current number of an outer loop
  • In each iteration of the inner loop, print the iterator variable of the inner loop (j)

Note:

  • In the first iteration of the outer loop inner loop execute five times.
  • In the second iteration of the outer loop inner loop execute four times.
  • In the last iteration of the outer loop, the inner loop will execute only once

Solution:

n = 5
k = 5
for i in range(0,n+1):
    for j in range(k-i,0,-1):
        print(j,end=' ')
    print()

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now