Nearby lessons

50 of 124

C - Pattern Programs

Classic C pattern programs — squares, right triangles, pyramids, diamonds, Pascal's triangle and number patterns. Every example uses nested loops, so patterns are the best possible loop practice.

The Universal Pattern Recipe

Every pattern program follows the same three-step structure. Once you see it, all patterns become the same problem:

  1. Outer loop — one iteration per row.
  2. Inner loop(s) — print the spaces and symbols for that row.
  3. Newline — printed in the outer loop, after the inner loop ends.
In simple words: ask yourself one question — "on row i, how many spaces and how many stars?" Turn that answer into a formula and the code writes itself.

1. Square of Stars

The simplest case: every row has the same number of columns, so the inner loop does not depend on i.

Example02
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int i, j, n = 4;
6 
7 for (i = 1; i <= n; i++) // rows
8 {
9 for (j = 1; j <= n; j++) // columns
10 printf("* ");
11 printf("\n"); // end of row
12 }
13 return 0;
14}
Output
* * * *
* * * *
* * * *
* * * * 

2. Left-Aligned Right Triangle

Now the inner loop runs to i instead of n, so row 1 gets one star, row 2 gets two, and so on:

Example03
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int i, j, n = 5;
6 
7 for (i = 1; i <= n; i++)
8 {
9 for (j = 1; j <= i; j++) // j <= i is the whole trick
10 printf("* ");
11 printf("\n");
12 }
13 return 0;
14}
Output
*
* *
* * *
* * * *
* * * * * 

3. Inverted Triangle

Count down instead of up — start the inner loop at i and run to n:

Example04
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int i, j, n = 5;
6 
7 for (i = 1; i <= n; i++)
8 {
9 for (j = i; j <= n; j++)
10 printf("* ");
11 printf("\n");
12 }
13 return 0;
14}
Output
* * * * *
* * * *
* * *
* *
* 

4. Pyramid — Spaces Plus Stars

A centred pyramid needs two inner loops: one for the leading spaces (n - i) and one for the stars (2*i - 1):

Example05
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int i, j, n = 5;
6 
7 for (i = 1; i <= n; i++)
8 {
9 for (j = 1; j <= n - i; j++) // leading spaces
10 printf(" ");
11 for (j = 1; j <= 2 * i - 1; j++) // stars: 1,3,5,7,9
12 printf("*");
13 printf("\n");
14 }
15 return 0;
16}
Output
    *
   ***
  *****
 *******
*********

5. Diamond

A diamond is a pyramid followed by an inverted pyramid. Two outer loops, same formulas:

Example06
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int i, j, n = 4;
6 
7 /* upper half including the widest row */
8 for (i = 1; i <= n; i++)
9 {
10 for (j = 1; j <= n - i; j++) printf(" ");
11 for (j = 1; j <= 2 * i - 1; j++) printf("*");
12 printf("\n");
13 }
14 /* lower half */
15 for (i = n - 1; i >= 1; i--)
16 {
17 for (j = 1; j <= n - i; j++) printf(" ");
18 for (j = 1; j <= 2 * i - 1; j++) printf("*");
19 printf("\n");
20 }
21 return 0;
22}
Output
   *
  ***
 *****
*******
 *****
  ***
   *

6. Hollow Square

Print a star only on the border — that is, when the row or column is first or last:

Example07
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int i, j, n = 5;
6 
7 for (i = 1; i <= n; i++)
8 {
9 for (j = 1; j <= n; j++)
10 {
11 if (i == 1 || i == n || j == 1 || j == n)
12 printf("* ");
13 else
14 printf(" ");
15 }
16 printf("\n");
17 }
18 return 0;
19}
Output
* * * * *
*       *
*       *
*       *
* * * * * 

7. Number Patterns

Swap the star for a number. Printing j counts across; printing i repeats the row number:

Example08
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int i, j, n = 4;
6 
7 printf("Pattern A - print j:\n");
8 for (i = 1; i <= n; i++) {
9 for (j = 1; j <= i; j++) printf("%d ", j);
10 printf("\n");
11 }
12 
13 printf("\nPattern B - print i:\n");
14 for (i = 1; i <= n; i++) {
15 for (j = 1; j <= i; j++) printf("%d ", i);
16 printf("\n");
17 }
18 return 0;
19}
Output
Pattern A - print j:
1
1 2
1 2 3
1 2 3 4

Pattern B - print i:
1
2 2
3 3 3
4 4 4 4 

8. Floyd's Triangle and Alphabet Pattern

Floyd's triangle keeps one counter running across all rows. The alphabet version adds the column number to 'A':

Example09
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int i, j, n = 4, num = 1;
6 
7 printf("Floyd's triangle:\n");
8 for (i = 1; i <= n; i++) {
9 for (j = 1; j <= i; j++) printf("%d ", num++);
10 printf("\n");
11 }
12 
13 printf("\nAlphabet pattern:\n");
14 for (i = 1; i <= n; i++) {
15 for (j = 0; j < i; j++) printf("%c ", 'A' + j);
16 printf("\n");
17 }
18 return 0;
19}
Output
Floyd's triangle:
1
2 3
4 5 6
7 8 9 10

Alphabet pattern:
A
A B
A B C
A B C D 

Pattern Formula Cheat Sheet

PatternSpaces on row iSymbols on row i
Square0n
Right triangle0i
Inverted triangle0n - i + 1
Pyramidn - i2*i - 1
Inverted pyramidi - 12*(n-i) + 1
Right-aligned trianglen - ii
The newline in the wrong place is the number-one pattern bug. If printf("\n") sits inside the inner loop, every symbol lands on its own line. It belongs in the outer loop, after the inner loop closes.
📝 Key Takeaways
  • The outer loop controls rows; the inner loop controls columns.
  • Stars in row i is usually i (triangle) or 2*i-1 (pyramid).
  • Leading spaces in a pyramid of n rows is n - i.
  • Print the newline in the outer loop, after the inner loop finishes.
  • Every pattern is just arithmetic on the row number.

🧠 Test Your Knowledge

4 Questions
Progress: 0 / 4