Nested Loops in C with Examples

Nested Loop refers to a loop within a loop, as the name implies. Within a loop, there can be any number of loops. We're all familiar with looping conditions such as for, while, and do-while. To create nested loops, we can loop multiple types of loops within one other. Nested Loops are supported by the C programming language.
Syntax
The syntax of Nested Loop in c is shown below.
1. Nested For Loop
Syntax: The syntax for nested for loop is shown below.
Flow chart: The flow chart for the nested for loop is shown below.

Example: The example for a nested for loop is shown below.
An annual examination in m subjects is given to a class of n students. A program that takes each student's marks in various subjects and calculates and prints the overall grade for each of them.
Output:
Explanation:
Two for loops are used in the program, one to handle the number of students and the other to handle the number of subjects. Because the program asks for both the number of students and the number of subjects, it can be used for any size class with any number of subjects.
The outer loop is divided into three sections, as follows:
- students' roll numbers are scanned out one by one;
- the inner loop, where each student's marks are scanned and totaled; and
- Total marks and division declarations are printed.
Transform Your Career
Choose from our industry-leading programs designed for career success
Modern Software and AI Engineering Program
Master full-stack development with AI integration
+1000 moreModern Data Science and ML with specialisation in AI
Advanced data science techniques with AI specialization
+1000 moreAdvanced AIML with Specialisation in Agentic AI
Deep dive into AIML with focus on Agentic systems
+1000 moreDevOps, Cloud & AI Platform Engineering
Build and manage AI-powered cloud infrastructure
+1000 moreAI Engineering Advanced Certification by IIT-Roorkee
Premier AI engineering certification from IIT-Roorkee
2. Nested While Loop
Syntax: The syntax for a nested while loop is shown below.
Flow chart: The flow chart for a nested while loop is shown below.

Example: The example for a nested while loop is shown below.
A program to print the 2-dimensional array using a while loop.
Output:
Explanation:
This program comprises two nested while loops. The variable row handles the outer loop. The variable cols handle the inner loop.
3. Nested Do-while Loop
Syntax: The syntax of a nested do-while loop is shown below.
Flow chart: The flow chart of a nested do-while loop is shown below.

Example: The example of a nested do-while loop is shown below.
A program to print the multiplication table from 1 x 1 to 10 x 10.
Output:
Explanation::
This program comprises two nested do...while loops. The variable row handles the outer loop, which is executed 10 times. The variable in the variable column handles the inner loop, which is run 10 times before the outer loop is run. That is, the inner loop is executed 100 times, printing a value in the table each time.
More Examples of Nested Loop in C
Turn Learning into Career Growth
Example 1
A C Program to print solid, hollow patterns using nested for loop.
Output:
Example 2
Pyramid pattern of number using nested while loop.
Output:
Example 3
A C program using a nested do-while loop.
Output:
Scaler Placement Report and Statistics
Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.
Example 4
A C Program to print pascal triangle using nested for loop.
Output:
Conclusion
- A nested loop is a loop statement that is included within another loop statement.
- All nested loops have been examined in terms of flow chart, syntax, and examples.
- The simplest way to understand how a nested loop works are to solve pattern printing problems.