Pattern Programs in C

Pattern programs are useful for learning nested loops. The outer loop usually controls the row, while an inner loop prints spaces, symbols or numbers inside that row.

Five Stars on One Line Top ↑

#include <stdio.h>

int main(void) {
    for (int i = 0; i < 5; i++) {
        putchar('*');
    }

    putchar('\n');
    return 0;
}

Output

*****

Decreasing Triangle Top ↑

#include <stdio.h>

int main(void) {
    for (int row = 5; row >= 1; row--) {
        for (int column = 1; column <= row; column++) {
            putchar('*');
        }

        putchar('\n');
    }

    return 0;
}

Output

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

Increasing Triangle Top ↑

#include <stdio.h>

int main(void) {
    for (int row = 1; row <= 5; row++) {
        for (int column = 1; column <= row; column++) {
            putchar('*');
        }

        putchar('\n');
    }

    return 0;
}

Output

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

Centered Pyramid Top ↑

#include <stdio.h>

int main(void) {
    const int rows = 5;

    for (int row = 1; row <= rows; row++) {
        for (int space = row; space < rows; space++) {
            putchar(' ');
        }

        for (int star = 1; star <= 2 * row - 1; star++) {
            putchar('*');
        }

        putchar('\n');
    }

    return 0;
}

Output

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

Reverse Centered Pyramid Top ↑

#include <stdio.h>

int main(void) {
    const int rows = 5;

    for (int row = rows; row >= 1; row--) {
        for (int space = row; space < rows; space++) {
            putchar(' ');
        }

        for (int star = 1; star <= 2 * row - 1; star++) {
            putchar('*');
        }

        putchar('\n');
    }

    return 0;
}

Output

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

Number Triangle Top ↑

#include <stdio.h>

int main(void) {
    for (int row = 1; row <= 5; row++) {
        for (int number = 1; number <= row; number++) {
            printf("%d", number);
        }

        putchar('\n');
    }

    return 0;
}

Output

1
12
123
1234
12345

Repeated-Number Triangle Top ↑

#include <stdio.h>

int main(void) {
    for (int row = 1; row <= 5; row++) {
        for (int column = 1; column <= row; column++) {
            printf("%d", row);
        }

        putchar('\n');
    }

    return 0;
}

Output

1
22
333
4444
55555

Diamond Pattern Top ↑

#include <stdio.h>

int main(void) {
    const int rows = 4;

    for (int row = 1; row <= rows; row++) {
        for (int space = row; space < rows; space++) {
            putchar(' ');
        }

        for (int star = 1; star <= 2 * row - 1; star++) {
            putchar('*');
        }

        putchar('\n');
    }

    for (int row = rows - 1; row >= 1; row--) {
        for (int space = row; space < rows; space++) {
            putchar(' ');
        }

        for (int star = 1; star <= 2 * row - 1; star++) {
            putchar('*');
        }

        putchar('\n');
    }

    return 0;
}

Output

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

How to Design Your Own Pattern Top ↑

  • Decide how many rows you need.
  • For each row, work out how many spaces or symbols appear.
  • Translate those counts into inner loops.
  • Print the newline only after the row is complete.

Review for loops and while loops before creating more complex patterns.

Preserved Original Pattern: Two Star Groups with a Growing Center Gap Top ↑

The original page contained several less-common double-sided patterns in addition to the standard pyramids. This keeps that exercise family instead of replacing it only with generic patterns.

#include <stdio.h>

int main(void) {
    const int width = 5;

    for (int row = width; row >= 1; row--) {
        for (int i = 0; i < row; i++) putchar('*');
        for (int gap = 0; gap < 2 * (width - row) + 1; gap++) putchar(' ');
        for (int i = 0; i < row; i++) putchar('*');
        putchar('\\n');
    }

    return 0;
}

Preserved Original Pattern: Expanding Two-Sided Shape Top ↑

#include <stdio.h>

int main(void) {
    const int width = 5;

    for (int row = 1; row <= width; row++) {
        for (int i = 0; i < row; i++) putchar('*');
        for (int gap = 0; gap < 2 * (width - row) + 1; gap++) putchar(' ');
        for (int i = 0; i < row; i++) putchar('*');
        putchar('\\n');
    }

    return 0;
}

Preserved Original Pattern: Combined Hourglass Shape Top ↑

#include <stdio.h>

void row(int stars, int width) {
    for (int i = 0; i < stars; i++) putchar('*');
    for (int gap = 0; gap < 2 * (width - stars) + 1; gap++) putchar(' ');
    for (int i = 0; i < stars; i++) putchar('*');
    putchar('\\n');
}

int main(void) {
    const int width = 5;

    for (int stars = width; stars >= 1; stars--) row(stars, width);
    for (int stars = 2; stars <= width; stars++) row(stars, width);

    return 0;
}

Preserved Original Numeric Pattern Top ↑

#include <stdio.h>

int main(void) {
    for (int step = 1; step < 10; step++) {
        for (int value = 1; value < 10; value += step) {
            printf("%d ", value);
        }
        putchar('\n');
    }

    return 0;
}




plus2net.com






✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer