This program will print a pattern of numbers from 1 to 8, where each row starts with the row number and the number of times it appears decreases with each subsequent column.
#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;
}
11111111
2222222
333333
44444
5555
666
77
8
The outer for
loop iterates through each row from 1 to 8, and the inner for
loop prints the row number i
the number of times equal to the number of columns left to be printed (j
).
So in the first row, the number 1 is printed 8 times, in the second row, the number 2 is printed 7 times, and so on, until in the last row, the number 8 is printed only once.