Pages

Showing posts with label pattern programs. Show all posts
Showing posts with label pattern programs. Show all posts

Thursday, 27 August 2026

C++ Solid Rectangle Pattern: Print a Rectangle of Stars Using Nested Loops

 

C++ rectangle star pattern program with full explanation
Image by Algocryption



Introduction:

If you are learning for loops in C++, printing patterns is a nice way to practice. You can see the result of your code immediately, which makes it easier to understand what each loop is doing.

In this program, we will print a solid rectangle made of stars. The user enters the number of rows and columns, and the program fills the rectangle with stars.

For example, if we enter 4 rows and 6 columns, the output will be:

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


There is nothing complicated happening here. we simply print a fixed number of stars on each row and repeat that row until we have the required number of rows.

This is also a good program to learn nested loops. once you understand how this works, many other pattern programs become easier.


Pattern Output:

Let's take this input:

4  6

Here, 4 means we want 4 rows, and 6 means we want 6 stars in every row.

The output is:

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


Let's count it:

  • Row 1 has 6 stars
  • Row 2 has 6 stars
  • Row 3 has 6 stars
  • Row 4 has 6 stars

So the rectangle contains:

4  ×  6  =  24  stars

If we change the input to:

3  5

We get:

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


The number of rows and columns can change, but the basic program stays the same.


Pattern Logic:

Before looking at the code, let's understand what the program has to do.

C++ rectangle pattern logic with 3 rows and 5 columns showing outer loop for rows and inner loop for stars


Suppose we want:

3 rows

5 columns

We can look at the rectangle like this:

          Column
        1 2 3 4 5

Row 1   * * * * *
Row 2   * * * * *
Row 3   * * * * *


The program needs to visit every row.

For each row, it needs to print 5 stars.

So there are two jobs:

First job: decide which row we are printing.

Second job: print all the stars in that row.

That's where the two loops come in.

The outer loop handles the rows.

The inner loop handles the stars in each row.

You can think of it this way:

First row  → print 5 stars

Second row → print 5 stars

Third row  → print 5 stars

After one row is finished, the program goes to the next line and starts again.


Square vs Rectangle:

Difference between square and rectangle star pattern in C++


This is also where a solid rectangle is different from a solid square.

A square might have:

5 rows

5 columns

A rectangle might have:

4 rows

6 columns

For a square, both numbers are usually the same.

For a rectangle, they can be different.


Algorithm:

Here are the steps for solving the problem:

1) Start the program.

2) Create two variables called rows and columns.

3) Read the number of rows.

4) Read the number of columns.

5) Start a loop to go through each row.

6) Inside that loop, start another loop to print the stars.

7) Print one star for each column.

8) After all stars in a row are printed, move to the next line.

9) Continue until all rows are finished.

10) Stop.


Pseudocode:


START


Read rows

Read columns


FOR i = 1 TO rows


    FOR j = 1 TO columns

        Print "* "

    END FOR


    Move to the next line


END FOR


STOP


The pseudocode is useful because you can understand the solution without worrying about C++ syntax.


C++ Program Implementation:

CPP
// Author: P.Prabhas
// Program: Solid Rectangle Pattern
// Website: Algocryption

#include <iostream>
using namespace std;

int main()
{
    // Store the number of rows and columns
    int rows, columns;

    // Read the number of rows
    cout << "Enter the number of rows: ";
    cin >> rows;

    // Read the number of columns
    cout << "Enter the number of columns: ";
    cin >> columns;

    // Print each row
    for (int i = 1; i <= rows; i++)
    {
        // Print stars in each column
        for (int j = 1; j <= columns; j++)
        {
            cout << "* ";
        }

        // Move to the next row
        cout << endl;
    }

    // End the program
    return 0;
}


Example Output:

Enter the number of rows: 4
Enter the number of columns: 6

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


Code Explanation:

Let's take the program one piece at a time.

Creating the Variables

int rows, columns;

We need two variables because we need two pieces of information.

rows tells the program how many lines to print.

Columns tells the program how many stars should be printed on each line.

For example:

rows = 4

columns = 6

Means:

Print 4 lines, and put 6 stars on every line.


Reading the input:

cin >> rows;

cin >> columns;

These statements read the values entered by the user.

If the user enters:

4

6

The program stores:

rows = 4

columns = 6


The outer Loop

for (int i = 1; i <= rows; i++)

This loop decides how many rows the rectangle will have.

If rows = 4, it runs four times:

i = 1

i = 2

i = 3

i = 4

Each time it runs, the program starts working on one row.


The Inner  Loop

for (int j = 1; j <= columns; j++)

This loop decides how many stars are printed in the current row.

If columns = 6, it runs six times.

So one row becomes:

*  *  *  *  *  *

After that, the inner loop starts again when the outer loop moves to the next row.


Printing the star

cout << "*  ";

This prints one star followed by a space.

Every time the inner loop runs, another star is printed.

For six iterations:

*  *  *  *  *  *


Moving to the Next line

cout << endl;

After the inner loop finishes, the program moves to the next line.

This is important because we want a new row instead of continuing on the same line.

Without endl , the stars would all appear in one long line.


Loop Dry Run:

Let's use a small example:

rows = 3

columns = 5


First row

The outer loop starts with:

i = 1

The inner loop runs 5 times:

j = 1 → *

j = 2 → *

j = 3 → *

j = 4 → *

j = 5 → *

The first row is:

*  *  *  *  *

The endl moves to the next line.


Second row

Now:

i = 2

The inner loop again runs 5 times:

*  *  *  *  *


Third row

Finally:

i = 3

The inner loop prints another 5 stars:

*  *  *  *  *

So the final output is:

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


The outer loop runs 3 times.

The inner loop runs 5 times for each row.


Time and Space Complexity:

Time Complexity

The outer loop runs rows times.

For every row, the inner loop runs columns times.

So the total number of star printing operations is:

rows × columns

Therefore, the time complexity is:

O(rows × columns)

For example, if:

rows = 4

columns = 6

The program prints:

4 × 6 = 24 stars


Space Complexity

The program only uses a few variables:

rows

columns

i

j

It does not create an array or store the complete rectangle.

So the extra space used by the program is constant.

Space Complexity: O(1)


Common Mistakes:


Using rows for Both Loops

A common mistake is writing:

for (int i = 1; i <= rows; i++)

{

    for (int j = 1; j <= rows; j++)

This works only when the number of row and columns happen to be the same.

For a rectangle, the inner loop should use columns:

for (int j = 1; j <= columns; j++)


Forgetting endl

If you remove:

cout << endl;

The program won't start a new row.

Instead of:

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


You will get all the stars on one line.


Using < instead of <=

Suppose:

Columns = 5

If you write:

j < columns

The loop runs only 4 times.

For this program, use:

j <= columns

So that all 5 columns are printed.


Mixing up Rows and Columns

Remember:

rows    → number of lines

columns → stars in each line


For example:

rows = 3

columns = 7


means:

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


Related Patterns:

Once you understand this program, you can try changing the loop logic to create other patterns.


Solid Square

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


Hollow Rectangle

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


The basic nested loop idea stays the same. you simply change what the program prints or how many characters it prints in each row.


Frequently Asked Questions (FAQ's):

What is a solid rectangle pattern in C++?

It is a rectangle in which every position is filled with the same character, usually a star.


How do I print a rectangle using nested loops?

Use one loop for the rows and another loop for the columns. Print a star inside the inner loop.


What is the difference between a square and a rectangle?

A square has the same number of rows and columns.

For example:

5  ×  5

A rectangle can have different values:

4  ×  6


Why are two loops used?

A rectangle has rows and columns. one loop moves through the rows, while the other moves through the columns.


Can i use while loops instead?

Yes. you can write the same program using nested while loops. the idea does not change.


Can i print something other than stars?

Yes. you can replace the * with numbers, letters, or another character.

For example:

cout << "#  ";

Will print a rectangle made of # symbols.


Saturday, 22 August 2026

C++ Hollow Square Pattern: Print a Square Border of Stars Using Nested Loops


C++ hollow square star pattern program with full explanation
Image by Algocryption





I
ntroduction:

When learning C++ loops, pattern programs are a good way to understand how loops work. the hollow square pattern is especially useful because it teaches two things at the same time: nested loops and conditions.

In a solid square, every position contains a star. in a hollow square, only the outside border contains stars. the middle part is empty.

For example, if the size is 5, we get:

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


This may look a little harder than a solid square, but the logic is actually simple. we just need to find out whether the current position is on the border or inside the square.


Pattern Output:

Suppose the user enters:

5

The output will be:

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


Let's look at this pattern carefully.

* * * * *   ← First row
*       *   ← Only first and last stars
*       *   ← Only first and last stars
*       *   ← Only first and last stars
* * * * *   ← Last row


The first row is completely filled.

The last row is also completely filled

In the middle rows, only the first and last positions contain stars.

Everything between them is empty. That is why it is called a hollow square.


Pattern Logic:

The easiest way to understand this pattern is to imagine a square with rows and columns.

Hollow square pattern logic in C++ showing i as row, j as column and the border condition



For n = 5, there are:

5 rows
5 columns

We can think of every position like this:

        Column
       1 2 3 4 5
Row 1  * * * * *
Row 2  *       *
Row 3  *       *
Row 4  *       *
Row 5  * * * * *


Now ask one simple question for every position:

Is this position on the border of the square?

If the answer is yes, print * .

If the answer is no, print spaces.

A position is on the border when it is :

In the first row
In the last row
In the first column
In the last column

In the program:

i represents the row
j represents the column
n is the size of the square

So the border condition is:

i == 1 || i == n || j == 1 || j == n

Let's understand it one part at a time.


First row

i == 1

When i is 1, we are printing the first row.

So every position in that row gets a star.

* * * * *

Last row

i == n

When i reaches n5 , we are printing the last row.

Again, every position gets a star.

* * * * *


First column

j == 1

This prints the star at the beginning of every middle row.

*


Last column

j == n

This prints the star at the end of every middle row.

   *

When all these conditions are combined, we get the complete border.



Algorithm :

1) Start the program.

2) Declare a variable n to store the size of the square.

3) Read the value of n.

4) Use an outer loop to print n rows.

5) For every row, use an inner loop to check n positions.

6) Check whether the current position is:
  • The first row
  • The last row
  • The first column or the last column.

7) If it is a border position, print * .

8) Otherwise, print spaces.

9) After finishing one row, move to the next line.

10) Repeat until all rows are printed.

11) Stop.



C++ program implementation:
CPP
// Author: P.Prabhas
// Program: Hollow Square Pattern
// Website: Algocryption

#include <iostream>
using namespace std;

int main()
{
    // Store the square size
    int n;

    // Read the size from the user
    cout << "Enter the size of the square: ";
    cin >> n;

    // Print each row
    for (int i = 1; i <= n; i++)
    {
        // Print each position in the row
        for (int j = 1; j <= n; j++)
        {
            // Print stars only on the border
            if (i == 1 || i == n || j == 1 || j == n)
            {
                cout << "* ";
            }
            else
            {
                // Print spaces inside the square
                cout << "  ";
            }
        }

        // Move to the next row
        cout << endl;
    }

    // End the program
    return 0;
}

Example Output:

Enter the size of the square: 5
* * * * *
*       *
*       *
*       *
* * * * *


Code Explanation:


First, we create a variable:

int n;

This stores the size of the square.

Then:

cin >> n;

reads the number entered by the user.

If the user enters:

5

The program needs to print a square with 5 rows and 5 columns.


The outer Loop

for (int i = 1; i <= n; i++)

This loop handles the rows.

For n = 5, it runs like this:

i = 1 → Row 1
i = 2 → Row 2
i = 3 → Row 3
i = 4 → Row 4
i = 5 → Row 5

So every time the outer loop runs, the program starts working on a new row.


The inner Loop

Inside the outer loop, we have:

for (int j = 1; j <= n; j++)

This loop checks every position in the current row.

For example, in row 2:

j = 1 → First position
j = 2 → Second position
j = 3 → Third position
j = 4 → Fourth position
j = 5 → Last position

At each position, the program checks:

if (i == 1 || i == n || j == 1 || j == n)

If the position is on the border, it prints:

cout << "* ";

Otherwise, it prints:

cout << "  ";

The two spaces keep the empty part inside the square.

After one row is complete:

cout << endl;

moves to the next line.

Then the next row starts.



Loop Dry Run:

Let's understand the program with:

n = 5


Row 1

Here:

i = 1

The condition:

i == 1

is true.

So every position in the first row prints a star.

* * * * *


Row 2

Now:

i = 2

The first position has:

j = 1

Since j == 1, it is on the border.

Print:

*

For j = 2, j = 3, j = 4, the positions are inside the square.

So spaces are printed.

When:

j = 5

j == n is true.

So another star is printed.

The complete row becomes:

*       *


Row 3 and Row 4

The same logic happens again:

*       *
*       *



Row 5

Here:

i = 5

Since i == n, this is the last row.

Every position prints a star:

* * * * *

So the complete output is:

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


Time & Space Complexity:


Time Complexity: O(n²)

The outer loop runs n times.

For every row, the inner loop also runs n times.

So the program checks:

n × n = n²

Positions.

Therefore:

Time Complexity = O(n²)



Space Complexity O(1)

The program only uses a few variables such as:

n
i
j

It does not use an array or store the pattern in memory.

Therefore:

Space Complexity = O(1)



Common Mistakes:


Mistake 1: Forgetting the last column


Some beginners write only

if (i == 1 || i == n || j == 1)

This misses the last column.

You also need:

j == n

The complete condition is:

i == 1 || i == n || j == 1 || j == n 



Mistake 2: Using && Instead of ||


For this pattern, use OR:

||

Do not replace all conditions with AND:

&&

We want to print a star if the position matches any one border condition.



Mistake 3: Printing the Wrong Number of Spaces


The star is printed with:

"* "

So the empty part should also use two spaces:

"  "

This helps keep the shape of the square correct.



Mistake 4: Forgetting to Move to the Next Line


After completing each row, use:

cout << endl;

Without it, the entire pattern will print on one line.


Related Patterns:

After understanding the hollow square, you can try these next.


Solid Square

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

Hollow Rectangle

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

Right Triangle

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

Number Square

1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1

The basic idea of loops remains similar. you mainly change the conditions or what you print.



Frequently Asked Questions (FAQ's):


What is a hollow square pattern?

It is a square where stars are printed only on the outside border, while the inside remains empty.


Why do we need nested loops?

One loop is used for rows, and another loop is used for positions or columns inside each row.


What is the main condition for this pattern?


i == 1 || i == n || j == 1 || j == n

This checks whether the current position is on the border.


Can I use while loops?

Yes. You can create the same pattern using nested while loops. The border condition stays the same.


What happens when n = 1?

The output is:

*

Because the only position is also part of the border.



Conclusion:

The hollow square pattern becomes easy once you understand one idea: print stars only on the border.

The first and last rows are completely filled with stars. in the middle rows, only the first and last positions contain stars.

Nested loops help us move through every row and every position. The if condition decides whether to print a star or leave the position empty.

The main condition to remember is:

i == 1 || i == n || j == 1 || j == n

Once this logic is clear, you can solve many other border based patterns, including hollow rectangle and more advanced C++ pattern problems.

Happy coding! 

Featured post

C++ Solid Rectangle Pattern: Print a Rectangle of Stars Using Nested Loops

  Image by Algocryption I ntroduction: If you are learning for loops in C++, printing patterns is a nice way to practice. You...

Popular Posts