Part of the series

Several example codes

~1 min read • Updated Oct 7, 2025

Program Overview

This Python program reads the number of rows n from the user.
It then prints a triangle of increasing numbers, aligned to the right, where each row contains one more number than the previous.


Python Code:


rows = int(input("Enter number of rows: "))
num = 1

for i in range(1, rows + 1):
    print(" " * (rows - i), end="")  # spacing for alignment
    for j in range(i):
        print(f"{num} ", end="")
        num += 1
    print()

Sample Output for 3 Rows:


  1  
 2 3  
4 5 6

Step-by-Step Explanation:

- The outer loop runs from 1 to rows
- Each row starts with spaces to align the triangle
- The inner loop prints i numbers, incrementing num each time
- The result is a clean, right-aligned triangle of sequential numbers


Written & researched by Dr. Shahin Siami