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
- Count rows. That's your outer loop:
for (i = 1; i <= n; i++). - Make a table of row number vs number of spaces and stars.
- Find the formula in terms of
iandn, and use it as the inner loop limits.
| Pattern | Spaces in row i | Stars in row i |
|---|---|---|
| Right triangle | 0 | i |
| Inverted triangle | 0 | n − i + 1 |
| Pyramid | n − i | 2i − 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
- Right triangle: in row 1 the inner loop runs 1 time, in row 2 it runs 2 times... because its limit is
i. printf("\n")after the inner loop moves to the next row.- 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 i | Spaces | Stars | Formula check |
|---|---|---|---|
| 1 | 3 | 1 | 4 − 1 = 3, 2×1 − 1 = 1 |
| 2 | 2 | 3 | 4 − 2 = 2, 2×2 − 1 = 3 |
| 3 | 1 | 5 | 4 − 3 = 1, 2×3 − 1 = 5 |
| 4 | 0 | 7 | 4 − 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
\nafter 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
| Mistake | What happens | Fix |
|---|---|---|
\n inside the star loop | Every star on its own line | Put \n after the inner loops |
Inner limit is a fixed number instead of i | A rectangle, not a triangle | Use a formula with i |
Printing "* " in a pyramid | Shape becomes wider and uneven | Match the space formula to the star width |
| Diamond lower half starts at m | The middle row is printed twice | Start the lower half at m − 1 |
Star Pattern Practice Questions
- Which loop controls the rows in a pattern program?
- How many stars are in row i of a right triangle?
- Write the spaces and stars formula for row i of a pyramid with n rows.
- How many stars are in the last row of a pyramid with 6 rows?
- Why does the lower half of the diamond start from m − 1?
Show answers
- The outer loop.
- i stars.
- Spaces = n − i, stars = 2i − 1.
- 11 (2 × 6 − 1).
- 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)
-
In a right triangle pattern, row 5 has how many stars?
A) 1 B) 4 C) 5 D) 9
-
In a pyramid of 5 rows, row 2 has how many spaces?
A) 2 B) 3 C) 4 D) 5
-
Stars in row i of a pyramid:
A) i B) 2i C) 2i − 1 D) n − i
-
Pattern programs mainly use:
A) switch B) Nested loops C) goto D) Recursion only
-
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
- C: 5.
- B: 3 (5 − 2).
- C: 2i − 1.
- B: nested loops.
- B: 5 (3 + 2).
Quick Revision for Exams and Interviews
Read this before your exam. The highlighted words are the answer.
Must-remember facts
| Structure | Outer loop rows; inner loops spaces + stars; \n after |
|---|---|
| Right triangle | Row i: i stars |
| Inverted triangle | Row i: n − i + 1 stars |
| Pyramid | n − i spaces, 2i − 1 stars |
| Diamond | Pyramid + reversed pyramid (from m − 1) |
| Method | Table 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.