Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

Print Inverted Right Triangle

Print a shrinking right triangle of stars in Python. Students trace a descending row width and keep the newline outside the inner character-printing loop.

IntermediatePhase 2 - Level 4: Pattern PrintingExample 2 of 13
print-inverted-right-triangle.py
1# Take n
2n = int(input("Enter number of rows: "))
3
4# Print inverted triangle
5for i in range(n, 0, -1):
6 for j in range(1, i + 1):
7 print("*", end="")
8 print()

Output

Enter number of rows: 5
*****
****
***
**
*

In this one

  • Shrink row widths
  • Use a descending range
  • Keep inner counting independent

How it works

  1. 1

    Visit widths from n down to one.

  2. 2

    Print the current number of stars.

  3. 3

    End each completed row with print().

Edge cases

Is there a zero-star row?

Is there a zero-star row?

No. The outer stop zero is excluded.

What if n = 0?

What if n = 0?

The outer range is empty.

What's going on

The outer range begins at n and decreases to one. Its current i is the number of stars required on that row.

The inner loop still counts upward, but only to the current width. For n equal to three, rows contain three, two, then one star. The two loop directions serve different jobs: row size shrinks while each row is printed left to right.

Is there a zero-star row? No. The outer stop zero is excluded.

What if n = 0? The outer range is empty.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

What changes between rows?

Pick an answer to check it.

Question 2

Is there a zero-star row?

Pick an answer to check it.

Questions people ask

Is there a zero-star row?

No. The outer stop zero is excluded.

What if n = 0?

The outer range is empty.

What changes between rows?

The number of printed stars decreases.