Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Unit 1 — Loop Patterns and Nested Loops

Lesson 26 of 32 in the free C Language notes on Siksha Sarovar, written by Rohit Jangra.

Loop Patterns and Nested Loops in C

Nested loops are loops placed inside other loops. They are commonly used for working with 2D data and printing patterns.

---

Nested Loop Syntax

for (int i = 1; i <= outer; i++) {
    for (int j = 1; j <= inner; j++) {
        /* body */
    }
}

The inner loop completes all its iterations for each single iteration of the outer loop.

---

Pattern Programs

Right-Angled Triangle of Stars

for (int i = 1; i <= 5; i++) {
    for (int j = 1; j <= i; j++) {
        printf("* ");
    }
    printf("\n");
}
/*
*
* *
* * *
* * * *
* * * * *
*/

Inverted Triangle

int n = 5;
for (int i = n; i >= 1; i--) {
    for (int j = 1; j <= i; j++) {
        printf("* ");
    }
    printf("\n");
}

Number Triangle

for (int i = 1; i <= 5; i++) {
    for (int j = 1; j <= i; j++) {
        printf("%d ", j);
    }
    printf("\n");
}
/*
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
*/

Pyramid of Stars

int n = 5;
for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n - i; j++) printf(" ");
    for (int j = 1; j <= 2 * i - 1; j++) printf("*");
    printf("\n");
}

---

Multiplication Table

for (int i = 1; i <= 10; i++) {
    for (int j = 1; j <= 10; j++) {
        printf("%4d", i * j);
    }
    printf("\n");
}

---

Loop Control in Nested Loops

/* break only exits the innermost loop */
for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
        if (j == 1) break;   /* exits inner loop only */
        printf("%d,%d ", i, j);
    }
}
/* Output: 0,0 1,0 2,0 */

To break out of nested loops, use a flag variable or goto:

int done = 0;
for (int i = 0; i < 5 && !done; i++) {
    for (int j = 0; j < 5; j++) {
        if (i == 2 && j == 2) { done = 1; break; }
        printf("%d,%d ", i, j);
    }
}