Print Square Star Pattern

Print Square Star Pattern

Easy C++ Pattern Printing 30 views
Explanation Complexity

Problem Statement

Print a square pattern of stars.
Real Life: Basic pattern for learning nested loops.

Input Format

An integer n (size of the square).

Output Format

Square pattern printed using *.

Example

4
****
****
****
****

Constraints

• n ≥ 1

• Use nested loops

Concept Explanation

A square pattern has the same number of rows and columns.
Each row prints the same number of stars.
This pattern is used to understand nested loops.

Step-by-Step Explanation

1.Take input n.

2.Use an outer loop for rows from 1 to n.

3.Inside it, use an inner loop for columns from 1 to n.

4.Print * in each column.

5.After finishing one row, move to the next line.

Concept Explanation

A square pattern has the same number of rows and columns.
Each row prints the same number of stars.
This pattern is used to understand nested loops.

Step-by-Step Explanation

1.Take input n.

2.Use an outer loop for rows from 1 to n.

3.Inside it, use an inner loop for columns from 1 to n.

4.Print * in each column.

5.After finishing one row, move to the next line.

Input / Output Format

Input Format
An integer n (size of the square).
Output Format
Square pattern printed using *.
Constraints
• n ≥ 1

• Use nested loops

Examples

Input:
4
Output:
**** **** **** ****

Example Solution (Public)

C++
void pattern_q1_square() {
    int n = 5;
    
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++) {
            cout << "* ";
        }
        cout << endl;
    }
}

Official Solution Code

void pattern_q1_square() {
    int n = 5;
    
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++) {
            cout << "* ";
        }
        cout << endl;
    }
}
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.