Print Solid Rectangle Star Pattern

Last Updated : 13 Mar, 2026

Given two integers n and m, print a solid rectangle pattern of stars with n rows and m columns. Each row has exactly m stars.

Examples:

Input: n = 3, m = 5

Output:

420851457

Input: n = 4, m = 2

Output:

420851458
Try it on GfG Practice
redirect icon

Using Nested Loops – O(n*m) Time and O(1) Space

The pattern can be printed using two nested loops. The outer loop runs once for each row, while the inner loop runs for each column in that row, printing stars. After printing all the stars in a row, a newline is added to move to the next row.

C++
using namespace std;

int main()
{
    // Number of rows and columns
    int n = 3, m = 5;

    // Loop through each row
    for (int i = 1; i <= n; i++)
    {

        // Loop through each column in the current row
        for (int j = 1; j <= m; j++)
        {
            cout << "* ";
        }

        // Move to the next row
        cout << "\n";
    }

    return 0;
}
Java
class GFG {

    public static void main(String[] args)
    {

        // Number of rows and columns
        int n = 3, m = 5;

        // Loop through each row
        for (int i = 1; i <= n; i++) {

            // Loop through each column in the current row
            for (int j = 1; j <= m; j++) {

                // Print a star
                System.out.print("* ");
            }

            // Move to the next row
            System.out.println();
        }
    }
}
Python
def main():
    # Number of rows and columns
    n, m = 3, 5

    # Loop through each row
    for i in range(1, n + 1):

        # Loop through each column in the current row
        for j in range(1, m + 1):
            # Print a star
            print("*", end=" ")

        # Move to the next row
        print()


if __name__ == "__main__":
    main()
C#
using System;

class GFG {
    static void Main()
    {
        // Number of rows and columns
        int n = 3, m = 5;

        // Loop through each row
        for (int i = 1; i <= n; i++) {
            // Loop through each column in the current row
            for (int j = 1; j <= m; j++) {
                // Print a star
                Console.Write("* ");
            }

            // Move to the next row
            Console.WriteLine();
        }
    }
}
JavaScript
// Number of rows and columns
let n = 3, m = 5;

// driver code

// Loop through each row
for (let i = 1; i <= n; i++) {

    // Loop through each column in the current row
    for (let j = 1; j <= m; j++) {
        
        // Print a star
        process.stdout.write("* "); 
    }

    // Move to the next row
    console.log(); 
}

Output
* * * * * 
* * * * * 
* * * * * 
Comment