Print Square Pattern

Welcome to the Course! In our Very First Lesson, we will learn to print Square Pattern and will see how minor change in the code results in different output. But before we begin, lets first learn nested loops Study Why you should Learn Pattern Printing before Learning Different Patterns?

Nested Loops:

Nested Loop means Loops inside loops. A loop is used when we want to repeat something. Nested Loops are used when we want to repeat a repeated pattern. You Must have learned Matrices in your Mathematics course. they have rows are columns. those can be printed using nested loops. Nested loops are used to create patterns by repeating a set of instructions multiple times. In the case of square patterns, we need two nested loops: an outer loop for the rows and an inner loop for the columns. The outer loop determines the number of rows, while the inner loop controls the number of columns within each row. By combining the iterations of these loops, we can create square patterns.

Code Examples:

C++ Code:

#include <iostream>

int main() {
    int n;
    std::cout << "Enter the size of the square pattern: ";
    std::cin >> n;

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            std::cout << "* ";
        }
        std::cout << std::endl;
    }

    return 0;
}

Python Code:

n = int(input("Enter the size of the square pattern: "))

for i in range(n):
    for j in range(n):
        print("*", end=" ")
    print()

Output:

For the input n = 5, the output of both codes will be:
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *

Explanation of the Code:

    1. We prompt the user to enter the size of the square pattern and store the value in the variable n.
    2. The outer loop, controlled by the variable i, runs from 0 to n-1, iterating over the rows of the square pattern.
    3. Inside the outer loop, the inner loop, controlled by the variable j, also runs from 0 to n-1, iterating over the columns of the square pattern.
    4. Within the inner loop, we print the ‘*’ symbol followed by a space using the cout statement (in C++) or the print function (in Python).
    5. After the inner loop completes, we print a newline character to move to the next row using cout << endl (in C++) or print() (in Python).
    6. This process repeats for each row until the square pattern is complete.

Modifying the Pattern:

To observe how the pattern changes, we can replace the ‘*’ symbol with the loop variables ‘i’ and ‘j’.

Modified C++ Code:

#include <iostream>

int main() {
    int n;
    std::cout << "Enter the size of the square pattern: ";
    std::cin >> n;

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            std::cout << i << " ";
        }
        std::cout << std::endl;
    }

    return 0;
}

Modified Python Code:

n = int(input("Enter the size of the square pattern: "))

for i in range(n):
    for j in range(n):
        print(i, end=" ")
    print()

Output:

For the input n = 5, the output of both codes will be:
0 0 0 0 0
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4

Explanation of the Modification:

By replacing the ‘*’ symbol with the loop variable ‘i’, we print the value of ‘i’ in each cell of the square pattern. As the outer loop progresses, the value of ‘i’ increases, resulting in an increasing pattern. Each row will have the same value because the inner loop completes before moving to the next row.

Your Task

To observe the effect of the inner loop variable ‘j’ on the pattern, you can replace the print statement within the inner loop with print(j, end=" ") in both the C++ and Python codes. By doing so, you will see that the columns of the pattern change according to the value of ‘j’.

Customizing the Square Pattern:

To customize the number of rows and columns in the square pattern, you can introduce a new variable ‘m’ and take its value from the user. Follow the steps below:
    1. Prompt the user to enter the number of rows (‘n’) and the number of columns (‘m’).
    2. Modify the inner loop to use ‘m’ instead of ‘n’.

C++ Code:

#include <iostream>

int main() {
    int n, m;
    std::cout << "Enter the number of rows: ";
    std::cin >> n;
    std::cout << "Enter the number of columns: ";
    std::cin >> m;

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            std::cout << "* ";
        }
        std::cout << std::endl;
    }

    return 0;
}

Python Code:

n = int(input("Enter the number of rows: "))
m = int(input("Enter the number of columns: "))

for i in range(n):
    for j in range(m):
        print("*", end=" ")
    print()

Output:

For the input n = 3 and m = 4, the output of both codes will be:
* * * *
* * * *
* * * *

Explanation of Customization:

By introducing a new variable ‘m’ and taking its value from the user, we can customize the number of rows and columns in the square pattern independently. The user can now specify the desired number of rows and columns, resulting in a rectangular pattern. I hope you have learned the basic pattern and it’s working. now the task and see how things change. Also change loop from 1 to <=n to Omit Zero. If you have any queries about it. Ask in the Comment Section. In Next Lesson We will Learn about Printing Basic Right Angle Triangle. See you Tomorrow. Happy Coding!

1 thought on “Print Square Pattern”

Leave a Comment