C Nested Loops, Star Patterns & 9 Core Practice Algorithms

โšก C (C17 / C23 Standard) ๐ŸŸข Lesson 9 ๐Ÿ“‚ Phase 05: Loops & Iterations ๐Ÿ“… 2026 Edition
๐Ÿ“Œ Covered in this in-depth guide: Nested Loops Architecture ยท Star Patterns (Triangles, Pyramids) ยท Number Patterns ยท 9 Practice Programs (Prime, Armstrong, Fibonacci, Factorial, Reverse)

Welcome to Phase 5 (Part 2): C Nested Loops, Pattern Printing & 9 Classical Algorithmic Programs Masterclass! Nested loops are loops placed inside other loops, forming the fundamental building blocks for working with 2D matrices, image pixel grids, game boards, and geometric patterns. In this comprehensive guide, you will master the 2D coordinate model $(i, j)$, step-by-step algorithms for printing star and number patterns, and construct 9 core interview and real-world mathematical algorithms from scratch.

1Nested Loops Mechanics & The 2D Grid Coordinate Model

In a nested loop, outer loop controls the rows ($i$), and inner loop controls the columns ($j$). For each single iteration of the outer loop, the inner loop executes its entire complete cycle:

2D Matrix Coordinates (Outer Loop i = Rows, Inner Loop j = Columns):
Row 0: (0,0) (0,1) (0,2) (0,3)
Row 1: (1,0) (1,1) (1,2) (1,3)
Row 2: (2,0) (2,1) (2,2) (2,3)

Time Complexity: O(Rows * Cols)
2Star Patterns & Number Patterns Step-by-Step

โญ The Universal Pattern Logic:

1. Outer loop i (1 to N): Rows count ni track chesthundhi.
2. Inner loop j (1 to i): Current row lo enni stars/numbers print cheyyalo decide chesthundhi.
3. printf("\n"): Inner loop complete ayyaka next row ki move avthundhi.

C โ€” Star Triangle, Inverted Triangle & Number Pattern โ–ถ Run Code
#include <stdio.h>

void printPatterns(int n) {
    // 1. Right-Angled Star Triangle
    printf("1. Right-Angled Star Triangle:\n");
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= i; j++) {
            printf("* ");
        }
        printf("\n");
    }

    // 2. Inverted Star Triangle
    printf("\n2. Inverted Star Triangle:\n");
    for (int i = n; i >= 1; i--) {
        for (int j = 1; j <= i; j++) {
            printf("* ");
        }
        printf("\n");
    }

    // 3. Number Half-Pyramid
    printf("\n3. Number Triangle:\n");
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= i; j++) {
            printf("%d ", j);
        }
        printf("\n");
    }
}

int main(void) {
    printPatterns(4);
    return 0;
}
3The 9 Classical Practice Programs (Formulas & Implementation)

Here is the complete implementation of the 9 foundational algorithmic problems requested in the curriculum:

๐Ÿ“Œ Core Mathematical Formulas & Logic:

โ€ข Sum of N: $Sum = sum i$
โ€ข Factorial: $N! = 1 imes 2 imes dots imes N$ (with $0! = 1$)
โ€ข Reverse Number: $rev = rev imes 10 + (n pmod{10})$; $n /= 10$
โ€ข Prime Number: Divisible only by 1 and itself ($sqrt{N}$ loop boundary)
โ€ข Armstrong Number (3-digit): $153 = 1^3 + 5^3 + 3^3 = 153$
โ€ข Fibonacci Series: $F_n = F_{n-1} + F_{n-2}$ (Starting with 0, 1, 1, 2, 3, 5, 8...)

C โ€” The 9 Classical Algorithmic Suite โ–ถ Run Suite
#include <stdio.h>
#include <stdbool.h>

int main(void) {
    // 1. Print 1 to 10 Numbers
    printf("1. Numbers (1-10): ");
    for (int i = 1; i <= 10; i++) printf("%d ", i);
    printf("\n");

    // 2. Multiplication Table (7 x i)
    printf("\n2. Multiplication Table for 7:\n");
    for (int i = 1; i <= 5; i++) printf("   7 x %d = %d\n", i, 7 * i);

    // 3. Sum of First 100 Numbers
    int sum = 0;
    for (int i = 1; i <= 100; i++) sum += i;
    printf("\n3. Sum of 1 to 100 = %d\n", sum);

    // 4. Factorial of 5 (5! = 120)
    long long fact = 1;
    for (int i = 1; i <= 5; i++) fact *= i;
    printf("4. Factorial of 5 = %lld\n", fact);

    // 5. Reverse Number & 6. Count Digits (Num = 9845)
    int num = 9845, temp = num, rev = 0, digits = 0;
    while (temp > 0) {
        rev = rev * 10 + (temp % 10);
        temp /= 10;
        digits++;
    }
    printf("5. Reverse of %d = %d\n", num, rev);
    printf("6. Total digits in %d = %d\n", num, digits);

    // 7. Prime Number Check (Num = 29)
    int checkPrime = 29;
    bool isPrime = (checkPrime > 1);
    for (int i = 2; i * i <= checkPrime; i++) {
        if (checkPrime % i == 0) { isPrime = false; break; }
    }
    printf("7. Is %d Prime? %s\n", checkPrime, isPrime ? "YES (Prime)" : "NO");

    // 8. Armstrong Number Check (Num = 153 -> 1^3 + 5^3 + 3^3 = 153)
    int armNum = 153, armTemp = armNum, armSum = 0;
    while (armTemp > 0) {
        int d = armTemp % 10;
        armSum += (d * d * d);
        armTemp /= 10;
    }
    printf("8. Is %d an Armstrong Number? %s\n", armNum, (armSum == armNum) ? "YES" : "NO");

    // 9. Fibonacci Series (First 8 Terms)
    int t1 = 0, t2 = 1, nextTerm;
    printf("9. Fibonacci (8 terms): ");
    for (int i = 1; i <= 8; i++) {
        printf("%d ", t1);
        nextTerm = t1 + t2;
        t1 = t2;
        t2 = nextTerm;
    }
    printf("\n");

    return 0;
}
๐Ÿ’ป Try It Yourself โ€” Test Prime & Fibonacci in Live C Compiler

Run this Fibonacci generator and Armstrong number checker in our online GCC compiler:

C (GCC Standard) โ–ถ Open C Compiler
#include <stdio.h>

int main(void) {
    int n = 10, t1 = 0, t2 = 1;
    printf("First %d Fibonacci numbers:\n", n);
    for (int i = 1; i <= n; ++i) {
        printf("%d, ", t1);
        int next = t1 + t2;
        t1 = t2;
        t2 = next;
    }
    printf("\n");
    return 0;
}
Open in Online C Compiler โ†’