Nested Loops in C with Examples

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

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.

Flowchart of Nested For Loop in C

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

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

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.

Flowchart of Nested While Loop in C

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.

Flowchart of Nested do-while Loop in C

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

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

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

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

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.
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more