C++ Code Example: simple pattern

This code prints a right-angled triangle made of numbers with 8 rows, where the first row has 1 number and each subsequent row has one additional number.

  • i and j are integer variables declared at the beginning of the main function.
  • The outer for loop runs from 1 to 8, with i incrementing by 1 each time.
  • The inner for loop runs from 1 to the current value of i, with j incrementing by 1 each time.
  • Inside the inner loop, the number i is printed to the console using the cout statement.
  • After the inner loop completes, a newline character is printed to start the next row on a new line using cout << endl;.
#include &lt;iostream&gt;
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