OneBite.Dev - Coding blog in a bite size

implement a two nested loop in C

Code snippet on how to implement a two nested loop in C

int i, j; 
for (i=0; i<5; i++) { 
    for (j=i; j<5; j++) { 
        printf("%d ",j); 
    } 
    printf("\n"); 
}

This code uses two nested loops. The outer loop is initialized with ‘i’ variable equal to 0, and it runs until ‘i’ is less than 5. Inside this loop, there is an inner loop which is initialized with ‘j’ variable equal to ‘i’. This inner loop runs until ‘j’ is less than 5. Every time the inner loop runs, it prints ‘j’ value, and after completing the inner loop, it prints a new line. Thus, when this code runs, it prints numbers from 0-4 in each line, as the value of ‘j’ is repeatedly incremented in each iteration of the inner loop.

c