C++ Code Example: simple pattern

Code Explanation

LineDescription
5Creates the variables i and j of type integer
7for loop with the range 0 to 9 and a counter variable called i. i is incremented by 1 in each loop pass.
8A second for loop, nested in the first one with the range 0 to i and the counter variable j. j is incremented by 1 in each loop pass.
9The counter variable i of the first for loop is output.
In the first loop pass only a 1 is output, because the second loop has the range 0 – 1. On the second pass, the counter variable of the first loop (i) is 2. Consequently, the second for loop has the range 0 – 2, which is why the number 2, 2 times is output. The number 3, 3 times and so on
11Each time the second for loop is processed, a new line is printed
#include <iostream>
using namespace std;

int main() {
    int i, j;
		
		for (i = 1; i <= 8; i++) {
			for (j = 1; j <= i; j++) {
				cout << i;
			}
			cout << endl;
		}

    return 0;
}
Output
1
22
333
4444
55555
666666
7777777
88888888