Number Pattern in C++

Number triangle in C++: row i prints 1..i. Same loops as the right-half pyramid, j instead of *.

BeginnerPattern ProgramsExample 5 of 6
number-pattern.cpp
1#include <iostream>
2using namespace std;
3
4int main() {
5 int rows;
6
7 cout << "Enter number of rows: ";
8 cin >> rows;
9
10 for (int i = 1; i <= rows; i++) {
11 for (int j = 1; j <= i; j++) {
12 cout << j << " ";
13 }
14 cout << endl;
15 }
16
17 return 0;
18}

Output

Enter number of rows: 5
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

What's going on

Same nested shape as the right-half pyramid, but print j instead of *. Row i prints 1 2 … i.

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

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

cout << j << " ";

Walk 5:

i=1: 1
i=2: 1 2
i=3: 1 2 3
i=4: 1 2 3 4
i=5: 1 2 3 4 5

cout << i instead of j and every row is the same digit: 1 / 22 / 333 / … That's a different homework.

j from i down to 1 prints 5 4 3 2 1 on the last row.

Spaces and centering are the left-half / full-pyramid trick. This program doesn't pad.