Pages

Showing posts with label Solid Square Pattern. Show all posts
Showing posts with label Solid Square Pattern. Show all posts

Monday, 17 August 2026

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



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



I
ntroduction  :

When you start learning loops in C++, pattern programs are a simple way to understand how repetition works. A solid square pattern is one of the best beginner programs because the logic is easy to see.

The program prints the same number of stars in every row. if the user enters 5, the program prints 5 rows, with 5 stars in each row.

The main concept used here is nested loops. one loop handles the rows, and another loop prints the stars inside each row.

Pattern Output :

For example, if the input is :

5

The output is :



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

This is called a solid square because every position is filled with a star.

For a square of size n :
  • Number of rows = n
  • Number of stars in each row = n
So, if n = 4, the pattern becomes:

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



Pattern Logic :

The logic is simple.

We need to print rows first. inside every row, we need to print the required number of stars.

That is why we use two loops.

The outer loop controls how many rows are printed :

Row 1
Row 2
Row 3
...
Row n

The inner loop prints the stars for each row.

C++ solid square pattern logic for n = 3 showing outer loop for rows and inner loop for stars

For example, when n = 3:

First row: print 3 stars
Second row: print 3 stars
Third row: print 3 stars

The result is:

* * *
* * *
* * *


In simple words, the outer loop says, "Create the next row". The inner loop says, "fill this row with stars".


Algorithm :

1) Start

2) Declare an integer variable n .

3) Ask the user to enter the size of the square .

4) Read the value of n .
 
5) Repeat from 1 to n to create each row .

6) For every row, repeat from 1 to n to print the stars

7) Print * in each inner loop iteration .

8) After printing all stars in one row, move to the next line .

9) Continue until all n rows are printed .

10) Stop


C++ Program Implementation:
C++
// Author: P.Prabhas
// Program: Solid 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 ( Outer Loop )
    for (int i = 1; i <= n; i++)
    {
        // Print stars in one row (Inner Loop )
        for (int j = 1; j <= n; j++)
        {
            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 called n :

int n;

This variable stores the size of the square.

Next :

cin  >>  n;

The user enters the size. if the user enters 5, the program needs to print a 5 × 5 square.

Now look at the outer loop:

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

This loop runs once for every row.

When n = 5, it runs 5 times. 

Inside it, we have another loop:

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

This loop prints the stars in the current row.

The statement :

cout << "*  ";

Prints one star each time the inner loop runs.

After the inner loop finishes, this line:

cout << endl;

moves to the next line. then the outer loop starts the next row.



Loop Dry Run :

Let's take :

n = 3

First outer loop

i = 1

The inner loop runs 3 times:

j = 1 → *
j = 2 → *
j = 3 → *

Output :

*   *   *

Second outer loop

i = 2

Again, the inner loop prints 3 stars:

*   *   *

Third outer loop

i = 3

The inner loop prints:

*   *   *

Final output:

*  *  *
*  *  *
*  *  *

The outer loop decides which row is being printed. the inner loop decides how many stars go into that row.



Time & Space Complexity :


Time Complexity : O(n²)

The outer loop runs n times.

For every outer-loop iteration, the inner loop also runs n times.

So the total number of stars-printing operations is:

n × n = n²

Therefore:

Time Complexity = O(n²)



Space Complexity: O(1)

The program only uses a few variables such as n , i , and j .

It does not use extra arrays or store the entire pattern in memory.

Therefore:

Space Complexity = O(1)



Common Mistakes :

Forgetting  cout << endl

If you remove:

cout << endl;

all the stars will print on one line instead of forming a square.


Using the wrong loop condition

For example :

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

This prints only  n - 1 stars because the loop stops before reaching n .

For an  n × n square, use :

j <= n 

Confusing i and j

The names themselves do not matter, but the roles should stay clear:

i handles rows

j handles star or columns



Related Patterns :

Once you understand the solid square, you can try similar C++ patterns:

Hollow Square

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

Solid Rectangle

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

Right Triangle

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

Number Square

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

These patterns use the same basic idea, but the loop conditions or printed values change



Frequently Asked Questions (FAQ's) :

What is a solid square pattern in C++ ?
 
It is a pattern where every row contains the same number of characters, and the number of rows is also the same. for a size of n , it creates an n × n square.

Why do we use nested loops ?

The pattern has rows and columns. The outer loop handles the rows, while the inner loop prints the stars in each row.

Can i use a while loop instead of a for loop ?

Yes. The same pattern can be printed using while loops. the main logic remains the same.

Can i print other characters instead of  * ?

Yes. You can replace * with numbers, letters, or any other character.

For example :

cout << "# ";

Will print a square using # .

What happens if n = 1 ?

*

A  1 × 1 square contains one row and one star.


Conclusion :

The solid square pattern is a simple C++ program, but it gives you good practice with nested loops.

The outer loop creates the rows. The inner loop prints the stars in each row. since both loops run n times, the program creates an n × n square.

The easiest way to remember the logic is :

One loop for rows
One loop for stars in each row

Once this becomes clear, you can move on to hollow squares, rectangles, triangles, pyramids, and other C++ pattern programs.

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