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 inputn = 5
, the output of both codes will be:
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
Explanation of the Code:
-
- We prompt the user to enter the size of the square pattern and store the value in the variable
n
. - The outer loop, controlled by the variable
i
, runs from 0 ton-1
, iterating over the rows of the square pattern. - Inside the outer loop, the inner loop, controlled by the variable
j
, also runs from 0 ton-1
, iterating over the columns of the square pattern. - Within the inner loop, we print the ‘*’ symbol followed by a space using the
cout
statement (in C++) or theprint
function (in Python). - After the inner loop completes, we print a newline character to move to the next row using
cout << endl
(in C++) orprint()
(in Python). - This process repeats for each row until the square pattern is complete.
- We prompt the user to enter the size of the square pattern and store the value in the variable
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 inputn = 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:-
- Prompt the user to enter the number of rows (‘n’) and the number of columns (‘m’).
- 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 inputn = 3
and m = 4
, the output of both codes will be:
* * * *
* * * *
* * * *
1 thought on “Print Square Pattern”