Print Solid Rectangle Pattern

Last Updated : 20 Oct, 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 Yourself
redirect icon

Nested Loops Approach - O(n * m) Time and O(1) Space

The pattern can be printed using two nested loops.

  • The outer loop iterates through the rows (1 to n)
  • The inner loop iterates through the columns (1 to m)

A star (*) is printed for every cell. After printing all stars in a row, move to the next line to form the solid rectangle.

Total number stars printed is equal to n * m

C++
#include <bits/stdc++.h>
using namespace std;

void printSolidRectangle(int n, int m)
{
    // Iterate through each row.
    for (int i = 1; i <= n; i++)
    {
        // Print m stars in the current row.
        for (int j = 1; j <= m; j++)
        {
            cout << "* ";
        }

        // Move to the next line after printing each row.
        cout << endl;
    }
}

int main()
{
    int n = 3, m = 5;
    printSolidRectangle(n, m);

    return 0;
}
Java
class GFG {
    static void printSolidRectangle(int n, int m)
    {
        // Iterate through each row.
        for (int i = 1; i <= n; i++) {

            // Print m stars in the current row.
            for (int j = 1; j <= m; j++) {
                System.out.print("* ");
            }

            // Move to the next line after printing each
            // row.
            System.out.println();
        }
    }

    public static void main(String[] args)
    {
        int n = 3, m = 5;
        printSolidRectangle(n, m);
    }
}
Python
def printSolidRectangle(n, m):
    # Iterate through each row.
    for i in range(1, n + 1):

        # Print m stars in the current row.
        for j in range(1, m + 1):
            print("* ", end="")

        # Move to the next line after printing each row.
        print()


# Driver Code
if __name__ == "__main__":
    n, m = 3, 5
    printSolidRectangle(n, m)
C#
using System;

class GFG {
    static void printSolidRectangle(int n, int m)
    {
        // Iterate through each row.
        for (int i = 1; i <= n; i++) {

            // Print m stars in the current row.
            for (int j = 1; j <= m; j++) {
                Console.Write("* ");
            }

            // Move to the next line after printing each
            // row.
            Console.WriteLine();
        }
    }

    static void Main()
    {
        int n = 3, m = 5;
        printSolidRectangle(n, m);
    }
}
JavaScript
function printSolidRectangle(n, m)
{
    // Iterate through each row.
    for (let i = 1; i <= n; i++) {

        // Print m stars in the current row.
        for (let j = 1; j <= m; j++) {
            process.stdout.write("* ");
        }

        // Move to the next line after printing each row.
        console.log();
    }
}


// Driver Code
const n = 3, m = 5;
printSolidRectangle(n, m);

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