Numbers in rectangular pattern in python

In this shot, we will discuss how to generate a rectangle pattern using numbers in Python.

Numerous patterns can be printed using Python once we have a strong grasp on concepts involving loops. Here, we will be using simple for loops to generate a rectangle pattern using numbers.

Description

To execute a rectangular pattern using Python programming, we will be using two for loops, one outer and one nested loop:

  • Outer loop: This is used to iterate over the number of rows.
  • Inner nested loop: This is used to iterate over the number of columns in each row.

Code

Let’s look at the below code snippet.

# Initialising Length and Breadth
rows = 3
columns = 6

# Loop through number of rows
for i in range(rows):
    
    # Loop through number of columns
    for j in range(columns):
        
        # Printing Pattern
        print(i+1, end = '  ')
    print()

Explanation

  • In line 2, we have taken the input for the number of rows.
  • In line 3, we have taken the input for the number of columns.
  • In line 6, we have created a for loop to iterate through the number of rows.
  • In line 9, we have created a for loop to iterate through the number of columns.
  • In line 12, i+1 has been used so as to start the pattern from 1. i increases with the increase in row number.

In this shot, we will discuss how to generate a hollow rectangle pattern with numbers in Python.

Numerous patterns can be printed using Python once you have a strong grip over the concepts involving loops. Here, we will use simple for loops to generate a rectangle pattern with numbers.

Description

To execute a rectangular pattern in Python, we will use 2 for loops:

  • Outer loop: Iterates over the number of rows.
  • Inner nested loop: Iterates over the number of columns in each row.

Code

Let us look at the code snippet below.

# Initialising Length and Breadth
rows = 3
columns = 6

# Loop through number of rows
for i in range(rows):
    
    # Loop through number of columns
    for j in range(columns):
        
        # Printing Pattern
        if(i == 0 or i == rows - 1 or j == 0 or j == columns - 1):
            print(i+1, end = '  ')
        else:
            print(' ', end = '  ')
    print()

Explanation

  • In line 2, we take the input for the number of rows.
  • In line 3, we take the input for the number of columns.
  • I line 6, we create a for loop to iterate through the number of rows.
  • In line 9, we create a for loop to iterate through the number of columns.
  • In line 13, we use i+1 to start the pattern from 1. i increases with an increase in row number.

This python program generates or prints rectangular pattern made up of stars up to n lines.

Python Source Code: Rectangular Pattern


# Rectangular pattern in Python

# Reading number of rows and columns
row = int(input("Enter number of rows: "))
col = int(input("Enter number of columns: "))

print("Rectangular pattern is: ")
for i in range(1,row+1):
    for j in range(1,col+1):
        print("*", end="")
    print()

Output: Rectangular Pattern

Enter number of rows: 4
Enter number of columns: 12
Rectangular pattern is: 

************
************
************
************

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Given a positive integer n, print the matrix filled with rectangle pattern as shown below: 
    a a a a a 
    a b b b a 
    a b c b a 
    a b b b a 
    a a a a a 
    where a = n, b = n – 1,c = n – 2 and so on.

    Examples: 

    Input : n = 4
    Output :
    4 4 4 4 4 4 4 
    4 3 3 3 3 3 4 
    4 3 2 2 2 3 4 
    4 3 2 1 2 3 4 
    4 3 2 2 2 3 4 
    4 3 3 3 3 3 4 
    4 4 4 4 4 4 4 
    
    Input : n = 3
    Output :
    3 3 3 3 3 
    3 2 2 2 3 
    3 2 1 2 3 
    3 2 2 2 3 
    3 3 3 3 3 

    For the given n, the number of rows or columns to be printed will be 2*n – 1. We will print the matrix in two parts. We will first print upper half from rows from 0 to floor((2*n – 1)/2) and then second half from floor((2*n – 1)/2) + 1 to 2*n – 2. 

    Now for each row, we will print it in three parts. First part is decreasing sequence which will start from n and decrease by 1 in each iteration. The number of iteration will be equal to row number, the second part is a constant sequence where constant is n – i and it will be print 2*n – 1 – 2 * row number, and the third part is increasing sequence which is nothing but opposite of the first sequence. 

    For lower half, observe, it is a mirror image of upper half (excluding middle row). So, simply run a loop of the upper half from (2*n – 1)/2 to 0.

    Below is the basic implementation of this approach: 

    C++

    #include <bits/stdc++.h>

    using namespace std;

    void printPattern(int n)

    {

        int s = 2 * n - 1;

        for (int i = 0; i < (s / 2) + 1; i++) {

            int m = n;

            for (int j = 0; j < i; j++) {

                cout << m << " ";

                m--;

            }

            for (int k = 0; k < s - 2 * i; k++) {

                cout << n - i << " ";

            }

            m = n - i + 1;

            for (int l = 0; l < i; l++) {

                cout << m << " ";

                m++;

            }

            cout << endl;

        }

        for (int i = s / 2 - 1; i >= 0; i--) {

            int m = n;

            for (int j = 0; j < i; j++) {

                cout << m << " ";

                m--;

            }

            for (int k = 0; k < s - 2 * i; k++) {

                cout << n - i << " ";

            }

            m = n - i + 1;

            for (int l = 0; l < i; l++) {

                cout << m << " ";

                m++;

            }

            cout << endl;

        }

    }

    int main()

    {

        int n = 3;

        printPattern(n);

        return 0;

    }

    Java

    import java.io.*;

    class GFG

    {

    static void printPattern(int n)

    {

        int s = 2 * n - 1;

        for (int i = 0;

                 i < (s / 2) + 1; i++)

        {

            int m = n;

            for (int j = 0; j < i; j++)

            {

                System.out.print(m + " ");

                m--;

            }

            for (int k = 0;

                     k < s - 2 * i; k++)

            {

                System.out.print(n - i + " ");

            }

            m = n - i + 1;

            for (int l = 0; l < i; l++)

            {

                System.out.print(m + " ");

                m++;

            }

            System.out.println();

        }

        for (int i = s / 2 - 1;

                 i >= 0; i--)

        {

            int m = n;

            for (int j = 0; j < i; j++)

            {

                System.out.print(m + " ");

                m--;

            }

            for (int k = 0;

                     k < s - 2 * i; k++)

            {

                System.out.print(n - i + " ");

            }

            m = n - i + 1;

            for (int l = 0; l < i; l++)

            {

                System.out.print(m + " ");

                m++;

            }

            System.out.println();

        }

    }

    public static void main (String[] args)

    {

        int n = 3;

        printPattern(n);

    }

    }

    Python3

    def printPattern(n) :

        s = 2 * n - 1

        for i in range(0, int(s / 2) + 1):

            m = n

            for j in range(0, i):

                print(m ,end= " " )

                m-=1

            for k in range(0, s - 2 * i):

                print(n-i ,end= " " )

            m = n - i + 1

            for l in range(0, i):

                print(m ,end= " " )

                m+=1

            print("")

        for i in range(int(s / 2),-1,-1):

            m = n

            for j in range(0, i):

                print(m ,end= " " )

                m-=1

            for k in range(0, s - 2 * i):

                print(n-i ,end= " " )

            m = n - i + 1

            for l in range(0, i):

                print(m ,end= " " )

                m+=1

            print("")

    if __name__=='__main__':

        n = 3

        printPattern(n)

    C#

    using System;

    class GFG

    {

    static void printPattern(int n)

    {

        int s = 2 * n - 1;

        for (int i = 0;

                 i < (s / 2) + 1; i++)

        {

            int m = n;

            for (int j = 0; j < i; j++)

            {

                Console.Write(m + " ");

                m--;

            }

            for (int k = 0;

                     k < s - 2 * i; k++)

            {

                Console.Write(n - i + " ");

            }

            m = n - i + 1;

            for (int l = 0; l < i; l++)

            {

                Console.Write(m + " ");

                m++;

            }

            Console.WriteLine();

        }

        for (int i = s / 2 - 1;

                 i >= 0; i--)

        {

            int m = n;

            for (int j = 0; j < i; j++)

            {

                Console.Write(m + " ");

                m--;

            }

            for (int k = 0;

                     k < s - 2 * i; k++)

            {

                Console.Write(n - i + " ");

            }

            m = n - i + 1;

            for (int l = 0; l < i; l++)

            {

                Console.Write(m + " ");

                m++;

            }

            Console.WriteLine();

        }

    }

    public static void Main ()

    {

        int n = 3;

        printPattern(n);

    }

    }

    PHP

    <?php

    function printPattern($n)

    {

        $s = 2 * $n - 1;

        for ($i = 0;

             $i < (int)($s / 2) + 1; $i++)

        {

            $m = $n;

            for ($j = 0; $j < $i; $j++)

            {

                echo $m , " ";

                $m--;

            }

            for ($k = 0; $k < $s - 2 * $i; $k++)

            {

                echo ($n - $i) , " ";

            }

            $m = $n - $i + 1;

            for ($l = 0; $l < $i; $l++)

            {

                echo $m , " ";

                $m++;

            }

            echo "\n";

        }

        for ($i = (int)($s / 2 - 1);

             $i >= 0; $i--)

        {

            $m = $n;

            for ($j = 0; $j < $i; $j++)

            {

                echo $m, " ";

                $m--;

            }

            for ($k = 0; $k < $s - 2 * $i; $k++)

            {

                echo $n - $i , " ";

            }

            $m = $n - $i + 1;

            for ($l = 0; $l < $i; $l++)

            {

                echo $m , " ";

                $m++;

            }

            echo "\n";

        }

    }

    $n = 3;

    printPattern($n);

    ?>

    Javascript

    <script>

    function printPattern(n)

    {

        let s = 2 * n - 1;

        for (let i = 0;

                 i < Math.floor(s / 2) + 1; i++)

        {

            let m = n;

            for (let j = 0; j < i; j++)

            {

                document.write(m + " ");

                m--;

            }

            for (let k = 0;

                     k < s - 2 * i; k++)

            {

                document.write(n - i + " ");

            }

            m = n - i + 1;

            for (let l = 0; l < i; l++)

            {

                document.write(m + " ");

                m++;

            }

            document.write("<br/>");

        }

        for (let i = Math.floor(s / 2) - 1;

                 i >= 0; i--)

        {

            let m = n;

            for (let j = 0; j < i; j++)

            {

                document.write(m + " ");

                m--;

            }

            for (let k = 0;

                     k < s - 2 * i; k++)

            {

                document.write(n - i + " ");

            }

            m = n - i + 1;

            for (let l = 0; l < i; l++)

            {

                document.write(m + " ");

                m++;

            }

            document.write("<br/>");

        }

    }

          let n = 3;

        printPattern(n);

    </script>

    Output

    3 3 3 3 3 
    3 2 2 2 3 
    3 2 1 2 3 
    3 2 2 2 3 
    3 3 3 3 3 
    

    Complexity Analysis:

    • Time Complexity: O(n2)
    • Auxiliary Space: O(1)

    Another Approach: 

    C++

    #include<bits/stdc++.h>

    using namespace std;

    void printPattern(int n)

    {

        int arraySize = n * 2 - 1;

        int result[arraySize][arraySize];

        for(int i = 0; i < arraySize; i++)

        {

            for(int j = 0; j < arraySize; j++)

            {

                if(abs(i - arraySize / 2) > abs(j - arraySize / 2))

                    result[i][j] = abs(i - arraySize / 2) + 1;

                else

                    result[i][j] = (abs(j-arraySize / 2) + 1);

            }

        }

        for(int i = 0; i < arraySize; i++)

        {

            for(int j = 0; j < arraySize; j++)

            {

                cout << result[i][j] << " ";

            }

            cout << endl;

        }

    }

    int main()

    {

        int n = 3;

        printPattern(n);

        return 0;

    }

    Java

    import java.io.*;

    class GFG

    {

    static void printPattern(int n)

    {

            int arraySize = n * 2 - 1;

            int[][] result = new int[arraySize][arraySize];

            for(int i = 0; i < arraySize; i++)

            {

                for(int j = 0; j < arraySize; j++)

                {

                    result[i][j] = Math.max(Math.abs(i-arraySize/2),

                                        Math.abs(j-arraySize/2))+1;

                }

            }

            for(int i = 0; i < arraySize; i++)

            {

                for(int j = 0; j < arraySize; j++)

                {

                System.out.print(result[i][j]);

                }

                System.out.println();

            }

    }

    public static void main (String[] args)

    {

        int n = 3;

        printPattern(n);

    }

    }

    Python3

    def printPattern(n):

        arraySize = n * 2 - 1;

        result = [[0 for x in range(arraySize)]

                     for y in range(arraySize)];

        for i in range(arraySize):

            for j in range(arraySize):

                if(abs(i - (arraySize // 2)) >

                   abs(j - (arraySize // 2))):

                    result[i][j] = abs(i - (arraySize // 2)) + 1;

                else:

                    result[i][j] = abs(j - (arraySize // 2)) + 1;

        for i in range(arraySize):

            for j in range(arraySize):

                print(result[i][j], end = " ");

            print("");

    n = 3;

    printPattern(n);

    C#

    using System;

    class GFG

    {

    static void printPattern(int n)

    {

            int arraySize = n * 2 - 1;

            int[,] result = new int[arraySize,arraySize];

            for(int i = 0; i < arraySize; i++)

            {

                for(int j = 0; j < arraySize; j++)

                {

                    result[i,j] = Math.Max(Math.Abs(i-arraySize/2),

                                        Math.Abs(j-arraySize/2))+1;

                }

            }

            for(int i = 0; i < arraySize; i++)

            {

                for(int j = 0; j < arraySize; j++)

                {

                    Console.Write(result[i,j]+" ");

                }

                Console.WriteLine();

            }

    }

    public static void Main (String[] args)

    {

        int n = 3;

        printPattern(n);

    }

    }

    PHP

    <?php

    function printPattern($n)

    {

        $arraySize = $n * 2 - 1;

        $result=array_fill(0,$arraySize,array_fill(0,$arraySize,0));

        for($i = 0; $i < $arraySize; $i++)

        {

            for($j = 0; $j < $arraySize; $j++)

            {

                if(abs($i - (int)($arraySize / 2)) >

                        abs($j - (int)($arraySize / 2)))

                    $result[$i][$j] = abs($i -

                            (int)($arraySize / 2)) + 1;

                else

                    $result[$i][$j] = (abs($j-(int)

                                    ($arraySize / 2)) + 1);

            }

        }

        for($i = 0; $i < $arraySize; $i++)

        {

            for($j = 0; $j < $arraySize; $j++)

            {

                echo $result[$i][$j]." ";

            }

            echo "\n";

        }

    }

        $n = 3;

        printPattern($n);

    ?>

    Javascript

    <script>

        function printPattern(n)

        {

                let arraySize = n * 2 - 1;

                let result = new Array(arraySize);

                for(let i = 0; i < arraySize; i++)

                {

                    result[i] = new Array(arraySize);

                    for(let j = 0; j < arraySize; j++)

                    {

                        result[i][j] = Math.max(Math.abs(i-parseInt(arraySize/2, 10)),

                                            Math.abs(j-parseInt(arraySize/2, 10)))+1;

                    }

                }

                for(let i = 0; i < arraySize; i++)

                {

                    for(let j = 0; j < arraySize; j++)

                    {

                        document.write(result[i][j] + " ");

                    }

                      document.write("</br>");

                }

        }

        let n = 3;

        printPattern(n);

    </script>

    Output

    3 3 3 3 3 
    3 2 2 2 3 
    3 2 1 2 3 
    3 2 2 2 3 
    3 3 3 3 3 
    

    Complexity Analysis:

    • Time Complexity: O(n2)
    • Auxiliary Space: O(n2) 

    How do you make a number pattern in Python?

    Pattern - 1: Number Pattern.
    rows = int(input("Enter the number of rows: ")).
    # Outer loop will print number of rows..
    for i in range(rows+1):.
    # Inner loop will print the value of i after each iteration..
    for j in range(i):.
    print(i, end=" ") # print number..
    # line after each row to display pattern correctly..
    print(" ").

    How do you square a number in Python?

    To calculate the square of a number in Python, we have three different ways..
    By multiplying numbers two times: (number*number).
    By using Exponent Operator (**): (number**2).
    Using math.pow() method: (math.pow(number, 2)).