![]() |
| 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.
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:
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:
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.




