Number patterns are commonly used to practice nested loops and understand how rows and columns are controlled in C++.
- Nested loops control the number of rows and values printed in each row.
- The value printed can depend on the row number, column number, or a separate counter.
Number Patterns
For n = 5, the three patterns are:
Pattern 1
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Pattern 2
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Pattern 3
1
12
123
1234
12345
Pattern 1: Continuous Number Pattern
Numbers are printed continuously from left to right. The number of values printed in each row is equal to the row number.
Using for Loop
The outer loop controls the rows, while the inner loop prints the required number of values. A separate number variable is incremented after every value.
#include <iostream>
using namespace std;
int main()
{
int rows, columns, number = 1, n = 5;
// first for loop is used to identify number of rows
for (rows = 0; rows <= n; rows++) {
// second for loop is used to identify number of
// columns and here the values will be changed
// according to the first for loop
for (columns = 0; columns < rows; columns++) {
// printing number pattern based on the number
// of columns
cout << number << " ";
// incrementing number at each column to print
// the next number
number++;
}
// print the next line for each row
cout << "\n";
}
return 0;
}
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Explanation: For every row, the inner loop prints as many numbers as the current row number. The number variable keeps increasing, so the sequence continues from one row to the next.
Using while Loop
The same pattern can be generated using nested while loops. The outer loop processes each row, while the inner loop prints the required number of values.
#include <iostream>
using namespace std;
int main()
{
int rows = 1, columns = 0, n = 5;
// 1 value is assigned to the number
// helpful to print the number pattern
int number = 1;
// while loops check the condition and repeat
// the loop until the condition is false
while (rows <= n) {
while (columns <= rows - 1) {
// printing number to get required pattern
cout << number << " ";
// incrementing columns value
columns++;
// incrementing number value to
// print the next number
number++;
}
columns = 0;
// incrementing rows value
rows++;
cout << endl;
}
return 0;
}
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Pattern 2: Repeated Row Number Pattern
In this pattern, each row contains the same number repeated multiple times. The row number determines both the value printed and the number of repetitions.
Using for Loop
The outer loop selects the row number, and the inner loop prints that row number row times.
#include <iostream>
using namespace std;
int main()
{
int rows, columns, n = 5;
// first for loop is used to
// identify number of rows
for (rows = 1; rows <= n; rows++) {
// second for loop is used to identify number of
// columns and here the values will be changed
// according to the first for loop
for (columns = 1; columns <= rows; columns++) {
// printing number pattern based on the number
// of rows
cout << rows << " ";
}
// print the next line for each row
cout << "\n";
}
return 0;
}
Output
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
Explanation: For row 1, the number 1 is printed once. For row 2, the number 2 is printed twice, and so on. Thus, both the value and the number of repetitions depend on the current row.
Using while loop
The while loops check the condition until the condition is false. If the condition is true then enter into the loop and execute the statements.
#include <iostream>
using namespace std;
int main()
{
int rows = 1, columns = 0, n = 5;
// while loops check the condition and repeat
// the loop until the condition is false
while (rows <= n) {
while (columns <= rows - 1) {
// printing number to get required pattern
cout << rows << " ";
// incrementing columns value
columns++;
}
columns = 0;
// incrementing rows value
rows++;
cout << endl;
}
return 0;
}
Output
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
Pattern 3: Increasing Number Pattern
In this pattern, every row starts from 1 and continues up to the current row number.
Using for Loop
The outer loop controls the rows, while the inner loop prints numbers from 1 to the current row number.
#include <iostream>
using namespace std;
int main()
{
int rows, columns, n = 5;
// second for loop is used to identify number of rows
for (rows = 1; rows <= n; rows++) {
// second for loop is used to identify number of
// columns and here the values will be changed
// according to the first for loop
for (columns = 1; columns <= rows; columns++) {
// print the number to be print based on the
// number of columns
cout << columns << " ";
}
// print the next line
cout << "\n";
}
return 0;
}
Output
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
Explanation: The inner loop starts from 1 for every row and prints values up to the current row number. Therefore, each successive row contains one additional number.
Using while loop
The while loops check the condition until the condition is false. If condition is true then enter into loop and execute the statements.
#include <iostream>
using namespace std;
int main()
{
int rows = 1, columns = 1, n = 6;
int number = 1;
// while loops check the condition and repeat
// the loop until the condition is false
while (rows <= n) {
while (columns <= rows - 1) {
// printing number to get required pattern
cout << columns << " ";
// incrementing columns value
columns++;
}
columns = 1;
// incrementing rows value
rows++;
cout << endl;
}
return 0;
}
Output
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5