;

Python Program to Print the Hollow Box Pattern of Binary Numbers


Tutorialsrack 16/05/2020 Python

In this Python program, we will learn how to print the box pattern of binary numbers 0 and 1.

Here is the source code of the program to print the box pattern of binary numbers 0 and 1.

Program 1: Python Program to Print the Hollow Box Pattern of Binary Numbers Using For Loop

Python Program to Print the Hollow Box Pattern of Binary Numbers Using For Loop
# Python Program to Print the Hollow Box Pattern of Binary Numbers
# Using For Loop

# Take the Input from the User
rows = int(input("Enter the total Number of Rows: "))
columns = int(input("Enter the total Number of Columns: "))

# Print the Output
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()
Output

Enter the total Number of Rows: 5

Enter the total Number of Columns: 5

Hollow Box Pattern of Numbers

1  1  1  1  1  

1              1  

1              1  

1              1  

1  1  1  1  1

Program 2: Python Program to Print the Hollow Box Pattern of Binary Numbers Using While Loop

Python Program to Print the Hollow Box Pattern of Binary Numbers Using While Loop
# Python Program to Print the Hollow Box Pattern of Binary Numbers
# Using While Loop

# Take the Input from the Output
rows = int(input("Enter the total Number of Rows: "))
columns = int(input("Enter the total Number of Columns: "))

# Print the Output
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()
Output

Enter the total Number of Rows: 4

Enter the total Number of Columns: 4

Hollow Box Pattern of Numbers

1  1  1  1  

1          1  

1          1  

1  1  1  1 


Related Posts



Comments

Recent Posts
Tags