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 three specific parts of the square:
- The first row
- The first column
- The secondary diagonal (from top-right to bottom-left)
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 row, first column, and secondary diagonal
elif 1 <= n <= 80:
    for i in range(n):
        row = ''
        for j in range(n):
            if i == 0 or 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 = 0 (shows secondary diagonal with n = 9):


*               
  *             
    *           
      *         
        *       
          *     
            *   
              * 
                *

Step-by-Step Explanation:

- If n = 0, the program uses n = 9 as a sample size
- Stars are printed only on the secondary diagonal (i + j == n - 1)
- If n is between 1 and 80, stars are printed on:
— First row (i == 0)
— First column (j == 0)
— Secondary diagonal (i + j == n - 1)
- All other positions are filled with spaces to maintain alignment


Written & researched by Dr. Shahin Siami