Skip to content
doubtly

2 answers

Best answer

Answer: Pattern 1 prints the column number j in row i; Pattern 2 (the modification) prints the row number i instead of j.

Logic: the outer loop i runs over rows 1 to r; the inner loop j runs from 1 to i. Printing j gives 1, 1 2, 1 2 3 …; changing the printed value to i gives 1, 2 2, 3 3 3 …

Program:

#include <stdio.h>

int main()
{
    int r, i, j;

    printf("Enter number of rows: ");
    scanf("%d", &r);

    /* Pattern 1: each row prints 1 to i */
    printf("Pattern 1\n");
    for (i = 1; i <= r; i++)
    {
        for (j = 1; j <= i; j++)
            printf("%d ", j);
        printf("\n");
    }

    /* Pattern 2 (modification): print the row number i, i times */
    printf("Pattern 2\n");
    for (i = 1; i <= r; i++)
    {
        for (j = 1; j <= i; j++)
            printf("%d ", i);
        printf("\n");
    }

    return 0;
}

Sample output (actual run):

Enter number of rows: 4
Pattern 1
1 
1 2 
1 2 3 
1 2 3 4 
Pattern 2
1 
2 2 
3 3 3 
4 4 4 4 
Doubtly · 23 Apr 2022 Report
Was this answer helpful?

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.

Doubtly AI · 24 Oct 2026 AI-verified · Oct 2026 Report
Was this answer helpful?

Your answer

Log in or create a free account to answer this question.