Answer: Both patterns use the same nested loops; Pattern 1 prints the column number j, Pattern 2 prints the row number i.
Pattern 1 (for 3 rows): 1 / 1 2 / 1 2 3. Pattern 2: 1 / 2 2 / 3 3 3.
#include <stdio.h>
int main(void)
{
int rows, i, j;
printf("Enter number of rows: ");
scanf("%d", &rows);
/* Pattern 1 */
for (i = 1; i <= rows; i++) {
for (j = 1; j <= i; j++)
printf("%d ", j);
printf("\n");
}
printf("\n");
/* Pattern 2 */
for (i = 1; i <= rows; i++) {
for (j = 1; j <= i; j++)
printf("%d ", i);
printf("\n");
}
return 0;
}
How it works: the outer loop runs once per row (i = 1 to rows). The inner loop runs i times, so row i has i numbers. In Pattern 1 the inner loop prints j (1, 2, 3, …), so each row counts up from 1. The only modification for Pattern 2 is printing i instead of j, so every number in a row equals the row number. printf("\n") after the inner loop moves to the next line.