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 two-part triangle pattern using asterisks (*).
The pattern consists of:
- An increasing triangle: rows from 1 to n
- A decreasing triangle: rows from n - 1 down to 1
If n = 0 is entered, the program defaults to n = 9 and prints a sample mirrored 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 pattern
if 1 <= n <= 80 or n == 9:
    # Increasing triangle
    for i in range(1, n + 1):
        print('* ' * i)

    # Decreasing triangle
    for i in range(n - 1, 0, -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
- The first loop prints rows with increasing number of stars
- The second loop prints rows with decreasing number of stars
- The result is a mirrored triangle pattern with a peak in the middle


Written & researched by Dr. Shahin Siami