Python Program to Print Same Numbers on all Sides of a Square

Write a Python program to print same numbers on all sides of a square pattern using for loop.

rows = int(input("Enter Square of same Numbers Pattern Rows = "))

print("====Print Same Numbers on all Sides of a Square Pattern====")

for i in range(1, rows + 1):
    for j in range(1, rows + 1):
        if i < j:
            print(rows - i + 1, end = ' ')
        else:
            print(rows - j + 1, end = ' ')
    for k in range(rows - 1, 0, - 1):
        if i < k:
            print(rows - i + 1, end = ' ')
        else:
            print(rows - k + 1, end = ' ')
    print()

for i in range(rows - 1, 0, -1):
    for j in range(1, rows + 1):
        if i < j:
            print(rows - i + 1, end = ' ')
        else:
            print(rows - j + 1, end = ' ')
    for k in range(rows - 1, 0, - 1):
        if i < k:
            print(rows - i + 1, end = ' ')
        else:
            print(rows - k + 1, end = ' ')
    print()
Python Program to Print Same Numbers on all Sides of a Square

This Python example prints the square pattern where all sides have the same numbers using a while loop.

rows = int(input("Enter Square of same Numbers Pattern Rows = "))

print("====Print Same Numbers on all Sides of a Square Pattern====")
i = 1
while(i <= rows):
    j = 1
    while(j <= rows):
        if i < j:
            print(rows - i + 1, end = ' ')
        else:
            print(rows - j + 1, end = ' ')
        j = j + 1
    k = rows - 1
    while(k >= 1):
        if i < k:
            print(rows - i + 1, end = ' ')
        else:
            print(rows - k + 1, end = ' ')
        k = k - 1
    print()
    i = i + 1

i = rows - 1
while(i >= 1):
    j = 1
    while(j <= rows):
        if i < j:
            print(rows - i + 1, end = ' ')
        else:
            print(rows - j + 1, end = ' ')
        j = j + 1
    k = rows - 1
    while(k >= 1):
        if i < k:
            print(rows - i + 1, end = ' ')
        else:
            print(rows - k + 1, end = ' ')
        k = k - 1
    print()
    i = i - 1
Enter Square of same Numbers Pattern Rows = 9
====Print Same Numbers on all Sides of a Square Pattern====
9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 
9 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 9 
9 8 7 7 7 7 7 7 7 7 7 7 7 7 7 8 9 
9 8 7 6 6 6 6 6 6 6 6 6 6 6 7 8 9 
9 8 7 6 5 5 5 5 5 5 5 5 5 6 7 8 9 
9 8 7 6 5 4 4 4 4 4 4 4 5 6 7 8 9 
9 8 7 6 5 4 3 3 3 3 3 4 5 6 7 8 9 
9 8 7 6 5 4 3 2 2 2 3 4 5 6 7 8 9 
9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 
9 8 7 6 5 4 3 2 2 2 3 4 5 6 7 8 9 
9 8 7 6 5 4 3 3 3 3 3 4 5 6 7 8 9 
9 8 7 6 5 4 4 4 4 4 4 4 5 6 7 8 9 
9 8 7 6 5 5 5 5 5 5 5 5 5 6 7 8 9 
9 8 7 6 6 6 6 6 6 6 6 6 6 6 7 8 9 
9 8 7 7 7 7 7 7 7 7 7 7 7 7 7 8 9 
9 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 9 
9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9