Part of the series

Several example codes

~3 min read • Updated Oct 6, 2025

Program Overview

This Python program reads an integer n between 0 and 80 and prints a square pattern of size n × n using asterisks (*).
The output highlights two specific parts of the square:
- The first column
- All elements on and above the secondary diagonal (i + j ≤ n - 1)
If n = 0 is entered, the program defaults to a sample size (e.g., 9) and displays only the secondary diagonal.


Python Code:


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

# Special case: n = 0 → show only secondary diagonal
if n == 0:
    n = 9  # default sample size
    for i in range(n):
        row = ''
        for j in range(n):
            if i + j == n - 1:
                row += '* '
            else:
                row += '  '
        print(row)

# Normal case: show first column and upper triangle of secondary diagonal
elif 1 <= n <= 80:
    for i in range(n):
        row = ''
        for j in range(n):
            if j == 0 or i + j <= n - 1:
                row += '* '
            else:
                row += '  '
        print(row)

else:
    print("The number must be between 0 and 80.")

Sample Output for n = 9:


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

Step-by-Step Explanation:

- If n = 0, the program uses n = 9 and prints only the secondary diagonal
- If n is between 1 and 80, stars are printed on:
— The first column (j == 0)
— On or above the secondary diagonal (i + j ≤ n - 1)
- All other positions are filled with spaces to maintain alignment


Written & researched by Dr. Shahin Siami