C++ Code Example: pattern reverse

Code Explanation

LineDescription
5Creates the variables i and j of type integer
7for loop with the range 1 to 8 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 8 to i and the counter variable j. j is decremented by 1 in each loop pass
9The counter variable i of the first for loop is output
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 = 8; j >= i; j--) {
            cout << i;
        }
        cout << endl;
    }

    return 0;
}
Output
11111111
2222222
333333
44444
5555
666
77
8