Skip to content
StudyFame

Pattern Programs in C

Star patterns are a favourite in C exams, lab tests and interviews. They look hard, but every pattern follows the same idea: an outer loop for rows, and inner loops for spaces and stars.

In this lesson you will learn a simple row formula trick that solves any pattern, and print 4 classic shapes: right triangle, inverted triangle, pyramid and diamond.

What You Will Learn

  • The 3-step method to solve any pattern.
  • Right triangle and inverted triangle.
  • Pyramid (centred triangle).
  • Diamond (pyramid + inverted pyramid).
  • How to find the formula for spaces and stars in each row.

Pattern Programs in C Explained

Exam definition: A pattern program prints characters (stars, numbers) in a shape using nested loops: the outer loop controls the rows, and one or more inner loops print the spaces and characters in each row.

The 3-step method

  1. Count rows. That's your outer loop: for (i = 1; i <= n; i++).
  2. Make a table of row number vs number of spaces and stars.
  3. Find the formula in terms of i and n, and use it as the inner loop limits.
PatternSpaces in row iStars in row i
Right triangle0i
Inverted triangle0n − i + 1
Pyramidn − i2i − 1

Memory trick: Every pattern = "Rows outside, Spaces then Stars inside, New line at the end" (R-S-S-N). Pyramid formula: spaces = n − i, stars = 2i − 1 (odd numbers 1, 3, 5, 7).

Syntax: The Pattern Template

for (int i = 1; i <= n; i++)          /* rows */
{
    for (int s = 1; s <= SPACES; s++)  /* spaces for row i */
        printf(" ");
    for (int j = 1; j <= STARS; j++)   /* stars for row i */
        printf("*");
    printf("\n");                      /* next row */
}

Example 1: Right Triangle and Inverted Triangle

#include <stdio.h>

int main(void)
{
    int n = 4;

    /* Right triangle: row i has i stars */
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= i; j++)
            printf("*");
        printf("\n");
    }

    printf("\n");

    /* Inverted triangle: row i has n - i + 1 stars */
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n - i + 1; j++)
            printf("*");
        printf("\n");
    }

    return 0;
}

Output

*
**
***
****

****
***
**
*

Code Explanation

  1. Right triangle: in row 1 the inner loop runs 1 time, in row 2 it runs 2 times... because its limit is i.
  2. printf("\n") after the inner loop moves to the next row.
  3. Inverted triangle: the limit is n - i + 1: row 1 → 4 stars, row 2 → 3, row 3 → 2, row 4 → 1.

How to Find the Pyramid Formula

For a pyramid with n = 4 rows, write down each row:

Row iSpacesStarsFormula check
1314 − 1 = 3, 2×1 − 1 = 1
2234 − 2 = 2, 2×2 − 1 = 3
3154 − 3 = 1, 2×3 − 1 = 5
4074 − 4 = 0, 2×4 − 1 = 7

Spaces go down by 1 each row → n - i. Stars go up by 2 each row → 2*i - 1. This "make a table, spot the pattern" method works for every shape.

Example 2: Pyramid and Diamond Pattern in C

#include <stdio.h>

int main(void)
{
    int n = 4;

    /* Pyramid */
    for (int i = 1; i <= n; i++)
    {
        for (int s = 1; s <= n - i; s++)
            printf(" ");
        for (int j = 1; j <= 2 * i - 1; j++)
            printf("*");
        printf("\n");
    }

    printf("\n");

    /* Diamond: pyramid of 3 rows + inverted pyramid of 2 rows */
    int m = 3;
    for (int i = 1; i <= m; i++)
    {
        for (int s = 1; s <= m - i; s++)
            printf(" ");
        for (int j = 1; j <= 2 * i - 1; j++)
            printf("*");
        printf("\n");
    }
    for (int i = m - 1; i >= 1; i--)
    {
        for (int s = 1; s <= m - i; s++)
            printf(" ");
        for (int j = 1; j <= 2 * i - 1; j++)
            printf("*");
        printf("\n");
    }

    return 0;
}

Output

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

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

A diamond is a pyramid followed by the same pyramid upside down. The second part simply runs i backwards (from m − 1 down to 1) with the same formulas.

Important Points to Remember

  • Outer loop = rows; inner loops = spaces, then stars.
  • Print \n after the inner loops.
  • Right triangle: i stars. Inverted: n − i + 1 stars.
  • Pyramid: n − i spaces, 2i − 1 stars.
  • Diamond = pyramid + reversed pyramid.

Common Mistakes Beginners Make

MistakeWhat happensFix
\n inside the star loopEvery star on its own linePut \n after the inner loops
Inner limit is a fixed number instead of iA rectangle, not a triangleUse a formula with i
Printing "* " in a pyramidShape becomes wider and unevenMatch the space formula to the star width
Diamond lower half starts at mThe middle row is printed twiceStart the lower half at m − 1

Star Pattern Practice Questions

  1. Which loop controls the rows in a pattern program?
  2. How many stars are in row i of a right triangle?
  3. Write the spaces and stars formula for row i of a pyramid with n rows.
  4. How many stars are in the last row of a pyramid with 6 rows?
  5. Why does the lower half of the diamond start from m − 1?
Show answers
  1. The outer loop.
  2. i stars.
  3. Spaces = n − i, stars = 2i − 1.
  4. 11 (2 × 6 − 1).
  5. So the widest middle row is not printed twice.

Coding Exercise

Print this number triangle for n = 4 (each row prints numbers from 1 to the row number):

1
12
123
1234
Show solution
#include <stdio.h>

int main(void)
{
    int n = 4;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= i; j++)
            printf("%d", j);
        printf("\n");
    }
    return 0;
}

Quick Quiz: Star Pattern Programs (MCQ)

  1. In a right triangle pattern, row 5 has how many stars?

    A) 1   B) 4   C) 5   D) 9

  2. In a pyramid of 5 rows, row 2 has how many spaces?

    A) 2   B) 3   C) 4   D) 5

  3. Stars in row i of a pyramid:

    A) i   B) 2i   C) 2i − 1   D) n − i

  4. Pattern programs mainly use:

    A) switch   B) Nested loops   C) goto   D) Recursion only

  5. A diamond with 3 rows in the top half has how many rows in total?

    A) 3   B) 5   C) 6   D) 7

Show answers
  1. C: 5.
  2. B: 3 (5 − 2).
  3. C: 2i − 1.
  4. B: nested loops.
  5. B: 5 (3 + 2).

Quick Revision for Exams and Interviews

Read this before your exam. The highlighted words are the answer.

Must-remember facts

StructureOuter loop rows; inner loops spaces + stars; \n after
Right triangleRow i: i stars
Inverted triangleRow i: n − i + 1 stars
Pyramidn − i spaces, 2i − 1 stars
DiamondPyramid + reversed pyramid (from m − 1)
MethodTable of row vs spaces/stars → formula

Frequently asked questions

How do you solve any pattern program?
Count rows, make a table of spaces and stars per row, find the formula in i and n.
What is the formula for a pyramid pattern?
Row i: n − i spaces and 2i − 1 stars.
Why are nested loops needed for patterns?
One loop for rows, another for the characters in each row.

Remember

  • Pattern recipe: R-S-S-N (Rows, Spaces, Stars, New line).
  • Pyramid stars are odd numbers: 1, 3, 5, 7.

Next Topic

Next lesson: Functions in C. Module 07 starts with functions: how to split a big program into small reusable parts.