For Statement

The for statement in C is a control flow statement used for looping, allowing you to execute a block of code multiple times. It’s commonly used when the number of iterations is known beforehand. It combines initialization, condition checking, and iteration in a single line.

for Syntax


for (initialization; condition; increment/decrement) {
    // Code to execute in each iteration
}

Example:1

#include <stdio.h>

int main() {
    for (int i = 0; i < 5; i++) {
        printf("%d/n", i);
    }
    return 0;
}
                

Output


0
1
2
3
4

Example 2: Sum of Numbers from 1 to N

#include <stdio.h>
int main() {
    int n, sum = 0;
    printf("Enter a positive integer: ");
    scanf("%d", &n);

    for (int i = 1; i <= n; i++) {
        sum += i;  // Add i to sum
    }

    printf("The sum of first %d numbers is: %d\n", n, sum);

    return 0;
}

Output

Enter a positive integer: 5 The sum of first 5 numbers is: 15

Nested for Loop

A nested for loop is a loop inside another loop. The outer loop will run first, and then the inner loop will run for each iteration of the outer loop.This is often used for multidimensional arrays or tables. The syntax for a nested for loop is as follows:

Syntax

#include <stdio.h>
for (initialization1; condition1; increment/decrement1) {   // Outer loop
    for (initialization2; condition2; increment/decrement2) {  // Inner loop
        // Code to execute in the inner loop
    }
    // Code to execute after the inner loop
}

Example 1: Multiplication Table

#include <stdio.h>
int main() {
    for (int i = 1; i <= 10; i++) {  // Outer loop for rows
        for (int j = 1; j <= 5; j++) {  // Inner loop for columns
            printf("%4d", i * j);  // Print the product of i and j
        }
        printf("\n");  // Move to the next row
    }

    return 0;
}

Output

1 2 3 4 5 2 4 6 8 10 3 6 9 12 15 4 8 12 16 20 5 10 15 20 25 6 12 18 24 30 7 14 21 28 35 8 16 24 32 40 9 18 27 36 45 10 20 30 40 50

We use Nested for loop for printing patterns. Some of the examples are given below:

Example 2: Square Pattern

#include <stdio.h>
int main() {
    int n;  
    printf("Enter the size of the square: ");
    scanf("%d", &\n);

    for (int i = 1; i <= n; i++) {  // Outer loop for rows
        for (int j = 1; j <= n; j++) {  // Inner loop for columns
            printf("* ");
        }
        printf("\n");
    }

    return 0;
}

Output(for n = 5):

* * * * * * * * * * * * * * * * * * * * * * * * *

Example 2: Right-Angled Triangle Pattern

#include <stdio.h>
int main() {
    int n;
    printf("Enter the number of rows: ");
    scanf("%d", &n);
    
    for (int i = 1; i <= n; i++) {    // Outer loop for rows
        for (int j = 1; j <= i; j++) { // Inner loop for columns
            printf("*");
        }
        printf("\n");  // Move to the next line after each row
    }

    return 0;
}

Output(for n = 5):

* ** *** **** *****

Example 3: Pyramid Pattern

#include <stdio.h>
int main() {
    int n;
    printf("Enter the number of rows: ");
    scanf("%d", &n);

    for (int i = 1; i <= n; i++) { // Outer loop for rows
        for (int j = 1; j <= n - i; j++) {  // Inner loop for spaces
            printf(" ");
        }
        for (int j = 1; j <= 2 * i - 1; j++) {  // Inner loop for stars
            printf("*");
        }
        printf("\n");  // Move to the next line after each row
    }

    return 0;
}

Output(for n = 5):

* *** ***** ******* *********

Example 4: Inverted Pyramid Pattern

#include <stdio.h>
int main() {
    int n;
    printf("Enter the number of rows: ");
    scanf("%d", &n);

    for (int i = n; i >= 1; i--) {  // Outer loop for rows
        for (int j = 1; j <= n - i; j++) {  // Inner loop for spaces
            printf(" ");
        }
        for (int j = 1; j <= 2 * i - 1; j++) {  // Inner loop for stars
            printf("*");
        }
        printf("\n");  // Move to the next line after each row
    }

    return 0;
}

Output(for n = 5):

********* ******* ***** *** *

A diamond pattern is a combination of the pyramid and inverted pyramid patterns. The upper half is a normal pyramid, and the lower half is an inverted pyramid.

Example 5: Diamond Pattern

#include <stdio.h>
int main() {
    int n;
    printf("Enter the number of rows: ");
    scanf("%d", &n);

    // Upper half of the diamond
    for (int i = 1; i <= n; i++) {  // Outer loop for upper half
        for (int j = 1; j <= n - i; j++) {  // Inner loop for spaces
            printf(" ");
        }
        for (int j = 1; j <= 2 * i - 1; j++) {  // Inner loop for stars
            printf("*");
        }
        printf("\n");
    }

    // Lower half of the diamond
    for (int i = n - 1; i >= 1; i--) {  // Outer loop for lower half
        for (int j = 1; j <= n - i; j++) {  // Inner loop for spaces
            printf(" ");
        }
        for (int j = 1; j <= 2 * i - 1; j++) {  // Inner loop for stars
            printf("*");
        }
        printf("\n");
    }

    return 0;
}

Output(for n = 5):

* *** ***** ******* ********* ******* ***** *** *

Number Patterns Using Nested for

Example 6: Simple Number Triangle

#include <stdio.h>
int main() {
    int n = 5;  // Number of rows
    for (int i = 1; i <= n; i++) {  // Outer loop for rows
        for (int j = 1; j <= i; j++) {  // Inner loop for numbers
            printf("%d ", j);
        }
        printf("\n");  // Newline after each row
    }
    return 0;
}

Output

1 1 2 1 2 3 1 2 3 4 1 2 3 4 5

Example 7: Repeated Number Pattern.

#include <stdio.h>
int main() {
    int n = 5;  // Number of rows
    for (int i = 1; i <= n; i++) {  // Outer loop for rows
        for (int j = 1; j <= i; j++) {  // Inner loop for numbers
            printf("%d ", i);
        }
        printf("\n");  // Newline after each row
    }
    return 0;
}

Output

1 2 2 3 3 3 4 4 4 4 5 5 5 5 5

Character Patterns Using Nested for

Example 8: Inverted Character Triangle.

#include <stdio.h>
int main() {
    int n = 5;  // Number of rows
    for (int i = n; i >= 1; i--) {
        for (char ch = 'A'; ch < 'A' + i; ch++) {
            printf("%c ", ch);  // Print characters in decreasing order of rows
        }
        printf("\n");
    }
    return 0;
}

Output

A B C D E A B C D A B C A B A

Example 9: Pyramid Character Pattern.

#include <stdio.h>
int main() {
    int n = 5;  // Number of rows
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n - i; j++) {  // Print spaces
            printf("  ");
        }
        for (char ch = 'A'; ch < 'A' + (2 * i - 1); ch++) {  // Print characters
            printf("%c ", ch);
        }
        printf("\n");
    }
    return 0;
}

Output

A A B C A B C D E A B C D E F G A B C D E F G H I