Nested Loop in C

Nested loops over Arrays in C programming refer to having one loop inside another loop.

This allows us to execute a set of instructions multiple times, where each iteration of the outer loop triggers a complete set of iterations of the inner loop.

Nested loops are commonly used for tasks such as iterating through multi-dimensional arrays or generating patterns.

Nested Loop Iteration:

#include <stdio.h>

int main() {
    int i, j;

    // Outer loop for rows
    for (i = 1; i <= 3; i++) {
        // Inner loop for columns
        for (j = 1; j <= 3; j++) {
            printf("%4d", i * j); // Print the product of row and column
        }
        printf("\n"); // Move to the next line after completing each row
    }

    return 0;
}

Output:

   1   2   3
   2   4   6
   3   6   9
  • the outer loop (for (i = 1; i <= 3; i++)) iterates through the rows, and the inner loop (for (j = 1; j <= 3; j++)) iterates through the columns.

  • Each iteration of the outer loop triggers 3 iterations of the inner loop, resulting in a complete row of the multiplication table being printed for each value of i.

Multiplication Table:

#include <stdio.h>

int main() {
    for (int i = 2; i <= 5; i+=2) { // Outer Loop
        for (int j = 1; j <= 10; j++) { // Inner Loop
            printf("%d * %d = %d\n", i, j, i * j);
        }
        printf("\n");
    }
    return 0;
}

Example:

Note: Above code prints the table of even numbers from 2 to 5.

Our Outer Loop starts executing from 2, and the inner loop iterates from 1 to 10. It will table of `2` and increment the outer loop value of `i` by 2.

Now, the Outer Loop `i` value is 4, it will skip iteration for value `i=3` because we have increment `i` by 2 in Outer Loop.

Next Iteration will print a table of `4` and increment the outer loop value of `i` by 2. Now, the outer loop `i` value is 6.

The next Iteration takes place and it will check the `6 < 5` condition will not verify and it comes out from the outer loop and starts executing the rest of the code after the loop.

Output:

2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
2 * 9 = 18
2 * 10 = 20

4 * 1 = 4
4 * 2 = 8
4 * 3 = 12
4 * 4 = 16
4 * 5 = 20
4 * 6 = 24
4 * 7 = 28
4 * 8 = 32
4 * 9 = 36
4 * 10 = 40

Finding Prime Numbers:

Example:

Note: Above code finds and prints all prime numbers up to a given number (20 in this example).

Example:

#include <stdio.h>
#include <stdbool.h>

int main() {
    int n = 20;
    bool isPrime;

    for (int i = 2; i <= n; i++) {
        isPrime = true;
        for (int j = 2; j <= i / 2; j++) {
            if (i % j == 0) {
                isPrime = false;
                break;
            }
        }
        if (isPrime)
            printf("%d ", i);
    }

    return 0;
}

Output:

2 3 5 7 11 13 17 19  // Prime Number

Note:

They are commonly used in various scenarios where repetitive tasks need to be performed multiple times, often with patterns or iterating through data structures like arrays.