Nested Loops in C – C allows loops to be nested, that is, one loop may be inside another. The program given below illustrates the nesting of loops. Let us consider a program to illustrate nested loops.
Program to generate the following pattern given below
1
1 2
1 2 3
1 2 3 4
/* Program to print the pattern */
#include <stdio.h>
main( )
{
int i,j;
for (i=1;i<=4;++i)
{
printf(“%d\n”,i);
for(j=1;j<=i;++j)
printf(“%d\t”,j);
}
}
Here, an inner for loop is written inside the outer for loop. For every value of i, j takes the value from 1 to i and then value of i is incremented and next iteration of outer loop starts ranging j value from 1 to i.