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.

Download code : nested-loop-1.c(4 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.

Download code: c-program-nested-loop-multiplication-table.c(4 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.

Program to plot various triangular patterns using nested for Loop.

Download code :nested-loop-pattern-triangle.c(4 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

Download code : nested-loop-triangle-pattern-2.c(5 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

Download Code : nested-loop-pyramid-pattern-3.c(4 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