Right-Aligned Descending Number Triangle in Python

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

What You’ll Learn

How to print a right-aligned descending number triangle in Python where each row begins with 5 and counts down: 5, 5 4, 5 4 3, and so on.

You’ll learn how to combine leading spaces with a descending number loop to form a neat triangle.

⭐ Pattern Output

For max_num = 5, the pattern looks like this:

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

Complete Python Program

Print spaces first to right-align the output, then print descending values from max_num down to the row limit.

Python
max_num = 5

for row in range(1, max_num + 1):
    # leading spaces (two spaces per missing number)
    for _ in range(max_num - row):
        print("  ", end=" ")

    # print: max_num, max_num-1, ..., max_num-row+1
    for val in range(max_num, max_num - row, -1):
        print(val, end=" ")
    print()

🧠 How It Works

1

Choose the maximum number

max_num = 5 decides both the height and the starting value for each row.

Setup
2

Outer loop (rows)

for row in range(1, max_num + 1) prints 1 number on the first row, 2 on the second, and so on.

Row control
3

Print leading spaces

The loop max_num - row prints indentation so numbers shift to the right on earlier rows.

Alignment
4

Print descending values

range(max_num, max_num - row, -1) prints 5, then 5 4, then 5 4 3, and so on.

Numbers
=

Right-aligned descending triangle

Total printed numbers are 1+2+…+n = n(n+1)/2, so time complexity is O(n²) for n rows.

2

Variation — User Input Version

Let the user choose the maximum number (and rows) at runtime:

Python
max_num = int(input("Enter the maximum number: "))

for row in range(1, max_num + 1):
    for _ in range(max_num - row):
        print("  ", end=" ")

    for val in range(max_num, max_num - row, -1):
        print(val, end=" ")
    print()

💡 Tips for Enhancement

Try These

  • Validate input (reject max_num < 1) before printing
  • Left-align the pattern by removing the space loop
  • Use fixed-width formatting like print(f"{val:2d}", end=" ") for cleaner alignment
  • Replace numbers with characters to build alphabet triangles
  • Invert the pattern by printing from 1 up to the row limit instead

Avoid

  • Forgetting print() after each row
  • Using inconsistent spacing (your triangle may look skewed)
  • Mixing row and column logic (keep loops single-purpose)
  • Assuming user input is always valid (wrap int() if needed)

Key Takeaways

1

Print leading spaces to make a right-aligned triangle.

2

Use a descending loop to print max_num down to the row limit.

3

The number of items per row increases by 1 each line (1, 2, 3, …).

4

Total prints are n(n+1)/2, giving O(n²) time.

❓ Frequently Asked Questions

It prints 1+2+3+4+5 = 15 numbers in total.
Yes—set max_num to your new starting value (like 7). The loop will print 7 on the first row, then 7 6, and so on.
Without spaces, the numbers will be left-aligned. Printing spaces first pushes the printed numbers to the right, forming the triangle shape.
O(n²) for n rows, because total output grows like 1+2+…+n.

Explore More Python Number Patterns!

From pyramids to Floyd’s triangle and beyond—practice nested loops with progressively richer patterns.

All Number Patterns →
Did you know?

Many right-aligned patterns are made simply by printing the right number of leading spaces. Once you can control indentation, you can build pyramids, diamonds, and more.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful