C Programs on Nested Loops – Examples

How Nested Loop works in C  ?

The placing of one loop inside the body of another loop is called nesting . The outer loop takes control of the number of complete repetitions of the inner loop. for every iteration of the outer loop, the inner loop will be executed completely.

#include<stdio.h>
int main() 
{
 
	int r,c; //counter variable declarations 

	r=1; //init. of outer loop counter 

	while ( r <= 3) // outer loop terminating condition 

	{ 
		printf("\n-- OUTER LOOP NO. %d",r); 
		c=1; //init. of inner loop counter 
		
		while(c <= 5) //inner loop terminating condition 
		{ 

			printf("\n\t-- INNER LOOP NO. %d",c); 
			c=c+1; // counter incrementation for inner loop 

		} 
		r=r+1; // counter incrementation for outer loop 
	} 
	printf("\n-----------------------------------digitalpadm.com\n");
	return 0; 

}
Download code : nested-loop-1.c (225 downloads)

In this example, Here total number of iterations of outer loop are 3 and inner loops are 5. for each iteration of outer loop, the inner loop will be executed 5 times. means when value of outer loop counter r is 1, c loop iterates from 1 to 5. for every value of r from 1 to 5 , c loops iterates completely.

Output

nested-loop-demo-1
nested-loop-demo-1

Note that, You can put any type of loop inside the body of another type of loop. as example, you can put a while loop inside the body of a for loop , or for loop inside do while and vice versa.

 

Let’s see , How to print multiplication table using nested looping.

#include<stdio.h>
int main() 
{
 
	int r,c,num; //counter variable declarations 

	r=1; //init. of outer loop counter 

	while ( r <= 10) // outer loop terminating condition 

	{ 
		
		c=1; //init. of inner loop counter 
		
		while(c <= 10) //inner loop terminating condition 
		{ 

			num=r*c;

			printf(" %3d",num);
			c=c+1; // counter incrementation for inner loop 

		} 
		r=r+1; // counter incrementation for outer loop 
		printf("\n");
	} 
	printf("\n-----------------------------------digitalpadm.com\n");
	return 0; 

}
Download code: c-program-nested-loop-multiplication-table.c (232 downloads)

Output

nested-loop-multiplication-table-c-program
nested-loop-multiplication-table-c-program

Here , numbers of rows and cols are 10. the values of r varies from 1 to 10 as row numbers and for each value of r , the c varies from 1 to 10. hence the multiplication of r & c gives the table number.


as compared to nested loop structure using while loop , it is easier to implement using for loop

lets see , above example using for loop.

#include<stdio.h>
int main() 
{
 
	int r,c,num; //counter variable declarations 

	r=1; //init. of outer loop counter 

	while ( r <= 10) // outer loop terminating condition 

	{ 
		
		c=1; //init. of inner loop counter 
		
		while(c <= 10) //inner loop terminating condition 
		{ 

			num=r*c;

			printf(" %3d",num);
			c=c+1; // counter incrementation for inner loop 

		} 
		r=r+1; // counter incrementation for outer loop 
		printf("\n");
	} 
	printf("\n-----------------------------------digitalpadm.com\n");
	return 0; 

}

Program to plot various triangular patterns using nested for Loop.

#include<stdio.h>
int main() 
{
 
	 int r,c,rows; //counter variable declarations 

	 r=1; //init. of outer loop counter 

	 rows=5; 

	 printf("\n\n");
	 for(r=1;r<=rows;r++) 
         {
               for(c=r;c<=rows;c++) 
   	       { 
  			printf("* "); 
	       } 
		printf("\n"); 
	 } 
	 printf("\n");
	 
 	 printf("\n-----------------------------------digitalpadm.com\n");
	
	 return 0; 
}
[wp_ad_camp_1]

 

Download code : nested-loop-pattern-triangle.c (226 downloads)

Explanation:

In this example,  inner loop counter is initialized by outer loop counter value. so when the value of r is 1 , inner loop varies from 1 to 5, means 5 times so 5 * prints on screen.

when the value of r is 2 , inner loop varies from 2 to 5, means 4 times so 4 * prints on screen.

when the value of r is 3 , inner loop varies from 3 to 5, means 3 times so 3 * prints on screen. in same way when r is 5 , inner loop executes once so single * prints on screen.

Output:

pattern-1-c-program
pattern-1-c-program

#include<stdio.h>
int main() 
{
 
	 int r,c,rows; //counter variable declarations 

	 r=1; //init. of outer loop counter 

	 rows=10; 

	 printf("\n\n");
	 for(r=1;r<=rows;r++) 
         {
               for(c=1;c<=r;c++) 
   	       { 
  			printf("* "); 
	       } 
		printf("\n"); 
	 } 
	 printf("\n");
	 
 	 printf("\n-----------------------------------digitalpadm.com\n");
	
	 return 0; 
}
Download code : nested-loop-triangle-pattern-2.c (198 downloads)

Output

In this example,  inner loop counter varies from 1 to every outer loop counter value. so when the value of r is 1 , inner loop varies from 1 to 1, means 1 times so single * prints on screen.

when the value of r is 2 , inner loop varies from 1 to 2, means 2 times so 2 * prints on screen.

when the value of r is 3 , inner loop varies from 1 to 3, means 3 times so 3 * prints on screen. in same way when r is 5 , inner loop executes 5 times so 5 * prints on screen.


pattern-2-c-program
pattern-2-c-program

C Program to Print Pyramid using Nested for Loop

#include<stdio.h>
int main() 
{
 
	 int r,c,k,rows; //counter variable declarations 

	 r=1; //init. of outer loop counter 

	 rows=10; 

	 printf("\n\n");
	 for(r=1;r<=rows;r++) 
         {

	       for(k=1;k<=rows-r;k++)
	       {
			printf("  ");
               }	
               for(c=1;c<=r;c++) 
   	       { 
  			printf("  * "); 
	       } 
		printf("\n"); 
	 } 
	 printf("\n");
	 
 	 printf("\n-----------------------------------digitalpadm.com\n");
	
	 return 0; 
}
Download Code : nested-loop-pyramid-pattern-3.c (219 downloads)

Explanation:

To print star in pyramid format, we need to insert a loop to add extra spaces before print star in a row. here loop with counter k varies from 1 to total number of rows minus row number. hence for first row ,total number of spaces are 10-1 as 9, & for second row it is 8 , third row 7 similarly for 9th row it is 1 and for 10th row its null.

Output:

pyramid-star-pattern-3-c-program
pyramid-star-pattern-3-c-program

 

 

Related programs on loops to learn more:

C Program Pascal Triangle Pattern Explanation
C Program – Loops Example – Fibonacci Series up to N terms
C Program –Loops Example -To Find Factorial of Number N