This article shows how to write a Python Program to Print a Rectangle Number Pattern using a for loop, while loop, and functions. This example uses the for loop to iterate rows and columns and print the Rectangle Number Pattern.
rows = int(input("Enter the Total Number of Rows : ")) columns = int(input("Enter the Total Number of Columns : ")) print("Rectangle Number Pattern") for i in range(1, rows + 1): for j in range(1, columns + 1): print(i, 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("Rectangle Number Pattern") for i in range(1, rows + 1): for j in range(1, columns + 1): print(j, end = ' ') print()
Output
Enter the Total Number of Rows : 7
Enter the Total Number of Columns : 10
Rectangle Number Pattern
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
The below example prints the same number in both rows and columns.
rows = int(input("Enter the Total Number of Rows : ")) columns = int(input("Enter the Total Number of Columns : ")) for i in range(1, rows + 1): for j in range(i, columns + i): print(j, end = ' ') print()
Output
Enter the Total Number of Rows : 5
Enter the Total Number of Columns : 5
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9
Python Program to Print Rectangle Number Pattern using while loop
This 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("Rectangle Number Pattern") i = 1 while i <= rows: j = 1 while j <= columns: print(i, end = ' ') j = j + 1 i = i + 1 print()
Output
Enter the Total Number of Rows : 8
Enter the Total Number of Columns : 14
Rectangle Number Pattern
1 1 1 1 1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3 3 3 3 3
4 4 4 4 4 4 4 4 4 4 4 4 4 4
5 5 5 5 5 5 5 5 5 5 5 5 5 5
6 6 6 6 6 6 6 6 6 6 6 6 6 6
7 7 7 7 7 7 7 7 7 7 7 7 7 7
8 8 8 8 8 8 8 8 8 8 8 8 8 8
In this Python example, we created a recNumbers function that accepts the rows, columns, and numbers to Print in Rectangle Pattern.
def recNumbers(rows,columns, n): for i in range(1, rows + 1): for j in range(1, columns + 1): print(n, end = ' ') print() rows = int(input("Enter the Total Number of Rows : ")) columns = int(input("Enter the Total Number of Columns : ")) n = int(input("Enter Any Number : ")) print("Rectangle Number Pattern") recNumbers(rows,columns, n)
Output
Enter the Total Number of Rows : 6
Enter the Total Number of Columns : 12
Enter Any Number : 9
Rectangle Number Pattern
9 9 9 9 9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9 9 9 9 9