Pattern Programs in C – 8 Star, Number and Alphabet Patterns with Code

Pattern programs in C use nested loops to print shapes made of characters — stars, numbers, or letters. They are among the most commonly set exercises in first-semester C programming courses because they teach how to control the exact number of characters printed per row by expressing it as a formula of the row number.

Every pattern follows the same three-step approach: (1) determine how many spaces and characters appear on each row, (2) express those counts as formulas in terms of the row number i, (3) translate each formula into an inner for loop. This page covers the eight most commonly asked pattern programs — right-angled triangle, inverted triangle, centered pyramid, diamond, number triangle, Floyd’s triangle, hollow square, and alphabet triangle — each with working C code, compiled output, and a formula table.

The Core Mental Model: Rows and Columns

Before writing any pattern, identify what changes per row:

What to count How to code it
Number of stars on row i Inner loop: for (j = 1; j <= <count>; j++)
Number of leading spaces on row i First inner loop prints spaces, second prints stars
Starting character changes each row Initialize before outer loop; update inside inner loop

Once you have the formula, writing the code is mechanical. The difficulty is always in finding the formula — not in the C syntax.


Pattern 1: Right-Angled Star Triangle

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

Formula: Row i has exactly i stars. No leading spaces.

Row (i) Stars
1 1
2 2
3 3
n n
/* Right-angled star triangle
 * Compile: gcc -ansi -Wall -Wextra triangle.c -o triangle */
#include <stdio.h>

int main(void)
{
    int n, i, j;
    printf("Enter number of rows: ");
    if (scanf("%d", &n) != 1 || n < 1 || n > 20) {
        printf("Enter a value between 1 and 20.\n"); return 1;
    }
    for (i = 1; i <= n; i++) {
        for (j = 1; j <= i; j++)
            printf("* ");
        printf("\n");
    }
    return 0;
}

Key insight: The inner loop condition j <= i ties the character count to the row number. When i=1, one star prints. When i=5, five stars print. This one-line relationship is the entire pattern logic.


Pattern 2: Inverted Right-Angled Triangle

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

Formula: Row i (counting from the top) has (n − i + 1) stars. Equivalently, start the outer loop at n and count down to 1.

/* Inverted right-angled triangle
 * Compile: gcc -ansi -Wall -Wextra inverted.c -o inverted */
#include <stdio.h>

int main(void)
{
    int n, i, j;
    printf("Enter number of rows: ");
    if (scanf("%d", &n) != 1 || n < 1 || n > 20) {
        printf("Enter a value between 1 and 20.\n"); return 1;
    }
    for (i = n; i >= 1; i--) {   /* count down — i is the star count */
        for (j = 1; j <= i; j++)
            printf("* ");
        printf("\n");
    }
    return 0;
}

Key insight: Running the outer loop in reverse (i = n; i >= 1; i--) is equivalent to starting with the biggest row and shrinking. The inner loop condition stays j <= i — unchanged from Pattern 1. The only difference is the direction of the outer loop.


Pattern 3: Centered Pyramid

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

Formula: Row i has (n − i) leading spaces and (2i − 1) stars. The star count is always odd and increases by 2 each row.

Row (i) Spaces (n−i) Stars (2i−1)
1 n−1 1
2 n−2 3
3 n−3 5
i n−i 2i−1
n 0 2n−1
/* Centered pyramid
 * Compile: gcc -ansi -Wall -Wextra pyramid.c -o pyramid */
#include <stdio.h>

int main(void)
{
    int n, i, j;
    printf("Enter number of rows: ");
    if (scanf("%d", &n) != 1 || n < 1 || n > 20) {
        printf("Enter a value between 1 and 20.\n"); return 1;
    }
    for (i = 1; i <= n; i++) {
        for (j = n; j > i; j--)       printf(" ");   /* n-i spaces */
        for (j = 1; j <= 2*i - 1; j++) printf("*"); /* 2i-1 stars */
        printf("\n");
    }
    return 0;
}

Key insight: Two inner loops — the first handles alignment (spaces), the second handles content (stars). The space loop runs n-i times because it starts at j=n and stops when j > i. The star loop uses the 2i-1 formula. These two inner loops together produce the centering effect.


Pattern 4: Diamond

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

Formula: A diamond is two pyramids joined at the widest row. The upper half is identical to the centered pyramid. The lower half mirrors it, running from row n−1 back down to row 1.

/* Diamond pattern
 * Compile: gcc -ansi -Wall -Wextra diamond.c -o diamond */
#include <stdio.h>

int main(void)
{
    int n, i, j;
    printf("Enter number of rows (half height): ");
    if (scanf("%d", &n) != 1 || n < 1 || n > 15) {
        printf("Enter a value between 1 and 15.\n"); return 1;
    }
    /* upper half: row 1 to n */
    for (i = 1; i <= n; i++) {
        for (j = n; j > i; j--)       printf(" ");
        for (j = 1; j <= 2*i - 1; j++) printf("*");
        printf("\n");
    }
    /* lower half: row n-1 down to 1 (mirror) */
    for (i = n - 1; i >= 1; i--) {
        for (j = n; j > i; j--)       printf(" ");
        for (j = 1; j <= 2*i - 1; j++) printf("*");
        printf("\n");
    }
    return 0;
}

Key insight: The lower half uses exactly the same two inner loops as the upper half. The only change is that the outer loop runs from n-1 down to 1 instead of from 1 up to n. The space and star formulas are unchanged — which is why the diamond is symmetric. A diamond with input n=5 has 9 rows total: 5 upper rows + 4 lower rows.


Pattern 5: Number Right Triangle

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

Formula: Row i prints the numbers 1 through i. The inner loop variable j is both the loop counter and the value to print.

/* Number right triangle
 * Compile: gcc -ansi -Wall -Wextra numtri.c -o numtri */
#include <stdio.h>

int main(void)
{
    int n, i, j;
    printf("Enter number of rows: ");
    if (scanf("%d", &n) != 1 || n < 1 || n > 20) {
        printf("Enter a value between 1 and 20.\n"); return 1;
    }
    for (i = 1; i <= n; i++) {
        for (j = 1; j <= i; j++)
            printf("%d ", j);   /* j is printed, not a star */
        printf("\n");
    }
    return 0;
}

Key insight: The only difference from the right-angled star triangle is printf("%d ", j) instead of printf("* "). The loop structure is identical. Every number triangle is a star triangle with the printed character replaced by the loop variable.


Pattern 6: Floyd’s Triangle

1
2  3
4  5  6
7  8  9  10
11 12 13 14 15

Formula: Row i contains exactly i consecutive integers. The integers run from 1 to n(n+1)/2 across all rows. A single counter variable tracks the current integer and is never reset between rows.

/* Floyd's triangle
 * Compile: gcc -ansi -Wall -Wextra floyd.c -o floyd */
#include <stdio.h>

int main(void)
{
    int n, i, j, num = 1;
    printf("Enter number of rows: ");
    if (scanf("%d", &n) != 1 || n < 1 || n > 20) {
        printf("Enter a value between 1 and 20.\n"); return 1;
    }
    for (i = 1; i <= n; i++) {
        for (j = 1; j <= i; j++)
            printf("%-4d", num++);   /* print and increment */
        printf("\n");
    }
    return 0;
}

Key insight: num is declared outside both loops and never reset. It persists across rows — that is what makes the numbers consecutive. The %-4d format (left-aligned in a 4-character field) keeps columns aligned when numbers reach two digits. If you want the numbers right-aligned, use %4d.


Pattern 7: Hollow Square

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

Formula: Print a star if the position is on any border (first row, last row, first column, or last column). Otherwise print spaces. This single condition replaces multiple loops.

/* Hollow square
 * Compile: gcc -ansi -Wall -Wextra hollow.c -o hollow */
#include <stdio.h>

int main(void)
{
    int n, i, j;
    printf("Enter side length: ");
    if (scanf("%d", &n) != 1 || n < 2 || n > 20) {
        printf("Enter a value between 2 and 20.\n"); return 1;
    }
    for (i = 1; i <= n; i++) {
        for (j = 1; j <= n; j++) {
            if (i == 1 || i == n || j == 1 || j == n)
                printf("* ");
            else
                printf("  ");   /* two spaces match "* " width */
        }
        printf("\n");
    }
    return 0;
}

Key insight: The border condition i == 1 || i == n || j == 1 || j == n elegantly handles all four sides of the square in a single if. The space in the else branch must be two characters (" ") to match the width of "* ", otherwise the hollow interior will be narrower than the border.


Pattern 8: Alphabet Right Triangle

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

Formula: Row i prints the first i letters of the alphabet. The character is derived by offsetting from 'A': 'A' + j where j runs from 0 to i−1.

/* Alphabet right triangle
 * Compile: gcc -ansi -Wall -Wextra alpha.c -o alpha */
#include <stdio.h>

int main(void)
{
    int n, i, j;
    printf("Enter number of rows (max 26): ");
    if (scanf("%d", &n) != 1 || n < 1 || n > 26) {
        printf("Enter a value between 1 and 26.\n"); return 1;
    }
    for (i = 1; i <= n; i++) {
        for (j = 0; j < i; j++)
            printf("%c ", 'A' + j);
        printf("\n");
    }
    return 0;
}

Key insight: Characters in C are integers. 'A' has ASCII value 65. Adding j to it gives the j-th letter of the alphabet: 'A'+0='A', 'A'+1='B', 'A'+25='Z'. The maximum rows is 26 because there are only 26 uppercase letters.


How to Solve Any Pattern Program in C

When you encounter an unfamiliar pattern, use this three-step method:

  1. Write out the pattern for n = 4 or 5 rows. Count exactly how many spaces and characters appear on each row. Write them as a table: Row 1 → 3 spaces, 1 star. Row 2 → 2 spaces, 3 stars. And so on.
  2. Express each count as a formula involving the row number i. Spaces: n−i. Stars: 2i−1. If the formula doesn’t fit immediately, look at differences between consecutive rows (spaces decrease by 1 each row → subtract i. Stars increase by 2 each row → multiply i by 2).
  3. Write one outer loop and one or two inner loops. The outer loop runs from i=1 to n (or in reverse). Each inner loop uses your formula as its upper bound. Print a newline after each outer iteration.

The common formulas to memorize:

Pattern type Leading spaces Characters per row
Right triangle 0 i
Inverted right triangle 0 n − i + 1 (or loop i from n to 1)
Centered pyramid n − i 2i − 1
Diamond (upper) n − i 2i − 1
Diamond (lower) n − i 2i − 1 (i from n−1 to 1)
Hollow square border condition (i==1 || i==n || j==1 || j==n)

Frequently Asked Questions

How do you print a star triangle in C?

Use two nested for loops. Outer loop: for (i = 1; i <= n; i++). Inner loop: for (j = 1; j <= i; j++) printf("* ");. After the inner loop: printf("\n");. This prints i stars on row i, producing a right-angled triangle that grows one star wider each row.

Why does the pyramid use 2i−1 stars?

A centered pyramid has one star on row 1, three on row 2, five on row 3 — always an odd number, increasing by 2 each row. The sequence 1, 3, 5, 7, 9 matches the formula 2i−1. The leading-space formula n−i ensures the widest row (row n) has zero leading spaces and sits flush left, while shorter rows are indented to create the centered effect.

Can I print pattern programs without nested loops?

Not in any readable way. Nested loops are the natural tool for 2D character output in C because the outer loop controls the row and the inner loop controls the column. You could replace the inner loop with repeated printf calls or a string buffer, but nested loops express the row-and-column relationship most directly and are the expected approach in any C course or interview.

Why is Floyd’s triangle called a triangle?

It forms a right-angled triangle shape: row 1 has one number, row 2 has two, row 3 has three. The numbers are consecutive integers starting from 1, never resetting between rows. It is named after Robert Floyd, the same computer scientist who invented the Floyd-Warshall shortest path algorithm.

What is the maximum n for the alphabet triangle?

26, because there are only 26 uppercase letters in the English alphabet (A through Z). If n exceeds 26, the expression 'A' + j with j ≥ 26 produces characters beyond ‘Z’ — technically legal in C but not meaningful alphabetically. Always validate that n ≤ 26 for alphabet patterns.

Related Programs

Recommended book:
The C Programming Language — Kernighan & Ritchie (India) |
(US)
 | 
C Programming: A Modern Approach — K.N. King (India) |
(US)

Test your understanding: C Aptitude Questions — or try our C Programming Quiz App on Android.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>