C – Nested For Loop

Nested for loop:

  • Defining for loop inside another for loop.
  • Nested loops are mainly used to process two-dimensional data (Rows & Columns)
  • Outer loop represents number of rows
  • Inner loop represents number of columns.

Following program prints the combinations of outer loop and inner loop variable values:

#include<stdio.h>
int main()
{
            int i, j;
            for(i=1 ; i<=5 ; i++)
            {
                        for(j=1 ; j<=5 ; j++)
                        {
                                    printf(“(%d,%d)\t”, i, j);
                        }
                        printf(“\n”);
            }
            return 0;
}
Output:
(1,1)   (1,2)   (1,3)   (1,4)   (1,5)
(2,1)   (2,2)   (2,3)   (2,4)   (2,5)
(3,1)   (3,2)   (3,3)   (3,4)   (3,5)
(4,1)   (4,2)   (4,3)   (4,4)   (4,5)
(5,1)   (5,2)   (5,3)   (5,4)   (5,5)
Scroll to Top