This article shows how to write a Python Program to Print a Hollow Rectangle Number Pattern using a for loop, while loop, and functions. This example uses the for loop to iterate rows and columns and the if else statement to print the Hollow Rectangle Number Pattern.
rows = int(input("Enter the Total Number of Rows : ")) columns = int(input("Enter the Total Number of Columns : ")) print("Hollow Rectangle Number Pattern") 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(i, end = ' ') else: print(' ', end = ' ') print()
Output.
As you can see from the above example, it prints the same number on each row. However, the below example prints the same number on each column.
rows = int(input("Enter the Total Number of Rows : ")) columns = int(input("Enter the Total Number of Columns : ")) print("Hollow Rectangle Number Pattern") 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(j, end = ' ') else: print(' ', end = ' ') print()
Output
Enter the Total Number of Rows : 9
Enter the Total Number of Columns : 10
Hollow Rectangle Number Pattern
1 2 3 4 5 6 7 8 9 10
1 10
1 10
1 10
1 10
1 10
1 10
1 10
1 2 3 4 5 6 7 8 9 10
This Python program replaces the for loop in the above example with a while loop to print the Rectangle Number Pattern.
rows = int(input("Enter the Total Number of Rows : ")) columns = int(input("Enter the Total Number of Columns : ")) print("Hollow Rectangle Number Pattern") i = 1 while i <= rows: j = 1 while j <= columns: if i == 1 or i == rows or j == 1 or j == columns: print(i, end = ' ') else: print(' ', end = ' ') j = j + 1 i = i + 1 print()
Output
Enter the Total Number of Rows : 5
Enter the Total Number of Columns : 8
Hollow Rectangle Number Pattern
1 1 1 1 1 1 1 1
2 2
3 3
4 4
5 5 5 5 5 5 5 5