Write a Python Program to Print Hollow Box Pattern of Numbers 1 and 0 using For Loop and While Loop with an example.
Python Program to Print Hollow Box Pattern of Numbers 1 and 0 using For Loop
This Python program allows the user to enter the total number of rows and columns. Next, we used a Python Nested For Loop to iterate over each row and column item. Within the loop, we used the Python If statement to check whether the row and column numbers are 1 or the maximum. If True, Python prints 1; otherwise, empty space.
# Python Program to Print Hollow Box Pattern of Numbers 1 and 0
rows = int(input("Please Enter the total Number of Rows : "))
columns = int(input("Please Enter the total Number of Columns : "))
print("Hollow Box Pattern of Numbers")
for i in range(1, rows + 1):
for j in range(1, columns + 1):
if(i == 1 or i == rows or j == 1 or j == columns):
print('1', end = ' ')
else:
print(' ', end = ' ')
print()

Python Program to Display Hollow Box Pattern of Numbers 1 and 0 using While Loop
This Python program to display a hollow box pattern is the same as the above. However, we replaced the For Loop in Python with While Loop in Python.
rows = int(input("Please Enter the total Number of Rows : "))
columns = int(input("Please Enter the total Number of Columns : "))
print("Hollow Box Pattern of Numbers")
i = 1
while(i <= rows):
j = 1;
while(j <= columns ):
if(i == 1 or i == rows or j == 1 or j == columns):
print('1', end = ' ')
else:
print(' ', end = ' ')
j = j + 1
i = i + 1
print()
Please Enter the total Number of Rows : 10
Please Enter the total Number of Columns : 17
Hollow Box Pattern of Numbers
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
>>>
Python Program to Display Hollow Box Pattern of Numbers 0 and 1
If you want to print a hollow box pattern of numbers 0 and 1, replace 1 in print statement with empty space and empty space with 1. For more patterns, please refer to the Python Number pattern programs.
rows = int(input("Please Enter the total Number of Rows : "))
columns = int(input("Please Enter the total Number of Columns : "))
print("Hollow Box Pattern of Numbers")
for i in range(1, rows + 1):
for j in range(1, columns + 1):
if(i == 1 or i == rows or j == 1 or j == columns):
print('0', end = ' ')
else:
print(' ', end = ' ')
print()
Please Enter the total Number of Rows : 12
Please Enter the total Number of Columns : 15
Hollow Box Pattern of Numbers
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
>>>
Also Read