Part of the series

Several example codes

~2 min read • Updated Oct 6, 2025

Program Overview

This Python program reads an integer n between 0 and 80 and prints a left-aligned triangle using asterisks (*).
Each row contains one more asterisk than the previous row, forming a right-angled triangle aligned to the left.
If n = 0 is entered, the program defaults to n = 9 and displays a sample triangle.


Python Code:


# Read n from user
n = int(input("Enter a number between 0 and 80: "))

# Special case: n = 0 → show sample triangle
if n == 0:
    n = 9  # default sample size

# Validate and print triangle
if 1 <= n <= 80 or n == 9:
    for i in range(1, n + 1):
        print('* ' * i)
else:
    print("The number must be between 0 and 80.")

Sample Output for n = 9:


*  
* *  
* * *  
* * * *  
* * * * *  
* * * * * *  
* * * * * * *  
* * * * * * * *  
* * * * * * * * *  

Step-by-Step Explanation:

- The user inputs a number n
- If n = 0, the program uses n = 9 as a default
- Each row prints i asterisks, where i is the row number
- The result is a left-aligned triangle of stars


Written & researched by Dr. Shahin Siami