Wednesday, November 21, 2018

Nested For Loop

1 comments

 

Nested For Loop


Nested for loop syntax: -
     for (initial value; test-condition; counter) {                  // outer loop
                for (initial value; test-condition; counter) {        // inner loop
                               statement;
                                   }
                                }
Example: -


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#include <stdio.h>
int main()
{
    int i, j, rows;

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

    for(i=1; i<=rows; ++i)
    {
        for(j=1; j<=i; ++j)
        {
            printf("%d ",j);
        }
        printf("\n");
    }
    return 0;
}

1 comment: