Skip to content
C project #06Beginner

Pattern Printer

Seven classic patterns — pyramid, diamond, Floyd's and Pascal's triangles, a number spiral and more — chosen from a menu.

Estimated time
~2 hours to build
Files
5 files · 331 lines
Technologies
C11, <stdio.h>

Introduction

Pattern problems are how most C courses teach nested loops, and they appear in nearly every lab exam. This project collects the important ones in one program and solves each with a short, readable function.

Problem statement

Build a menu that prints any of seven patterns at a size the user chooses (1–12): right triangle, pyramid, diamond, hollow square, Floyd's triangle, Pascal's triangle and a clockwise number spiral.

Objectives

  • Derive the number of spaces and symbols per row
  • Use nested loops with dependent limits
  • Compute binomial coefficients incrementally
  • Fill a 2D array in spiral order using boundary variables

Features

  • Seven patterns selectable from a menu
  • Pascal's triangle computed without factorials
  • Spiral matrix filled with shrinking boundaries
  • An array of function pointers maps menu choices to patterns

Concepts demonstrated

  • Nested loops
  • 2D arrays
  • Function pointers
  • Combinatorics

System architecture

Each pattern is a void f(int n) function. main stores them in an array of function pointers alongside their names, so the menu is a loop over that array and the chosen pattern is called through patterns[choice - 1](size).

Folder structure

TextProject tree
pattern-printer/
├── README.md
├── Makefile
├── src/main.c
├── tests/expected-output.txt
└── tests/sample-input.txt

Complete source code

Makefile

20 lines
MakefileMakefile
# Generated for KENHOLT Skills. Usage: make | make test | make clean
CC = gcc
CFLAGS = -std=c11 -Wall -Wextra -O2
SRC := $(wildcard src/*.c)
HDR := $(wildcard src/*.h)
ARGS := $(shell cat tests/args.txt 2>/dev/null)
INPUT := $(wildcard tests/sample-input.txt)
app: $(SRC) $(HDR)
$(CC) $(CFLAGS) -Isrc $(SRC) -o app -lm
test: app
./app $(ARGS) $(if $(INPUT),< $(INPUT),) > actual.txt
diff actual.txt tests/expected-output.txt && echo "Test passed: output matches tests/expected-output.txt"
clean:
rm -f app app.exe actual.txt
.PHONY: test clean

What each file does

src/main.c
One function per pattern plus the menu; pascal uses C(i, k+1) = C(i, k) × (i − k) / (k + 1).
Makefile
Build automation: make builds ./app, make test runs the sample session and compares it with the expected output, make clean removes build files.

How to run it

Build

Terminal
gcc -std=c11 -Wall -Wextra -Isrc src/*.c -o app -lm
# or simply: make

Run

Terminal
./app # Linux / macOS
.\app.exe # Windows

Test against the expected output

Terminal
./app < tests/sample-input.txt > actual.txt
diff actual.txt tests/expected-output.txt # no output means they match
# Windows PowerShell:
# Get-Content tests\sample-input.txt | .\app.exe > actual.txt
# Compare-Object (Get-Content actual.txt) (Get-Content tests\expected-output.txt)

Expected output

Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.

Texttests/expected-output.txt
=== Pattern Printer ===
1. Right triangle
2. Pyramid
3. Diamond
4. Hollow square
5. Floyd's triangle
6. Pascal's triangle
7. Number spiral
0. Exit
Choice:
Size (1-12):
Pyramid, size 4:
*
***
*****
*******
=== Pattern Printer ===
1. Right triangle
2. Pyramid
3. Diamond
4. Hollow square
5. Floyd's triangle
6. Pascal's triangle
7. Number spiral
0. Exit
Choice:
Size (1-12):
Diamond, size 3:
*
***
*****
***
*
=== Pattern Printer ===
1. Right triangle
2. Pyramid
3. Diamond
4. Hollow square
5. Floyd's triangle
6. Pascal's triangle
7. Number spiral
0. Exit
Choice:
Size (1-12):
Pascal's triangle, size 5:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
=== Pattern Printer ===
1. Right triangle
2. Pyramid
3. Diamond
4. Hollow square
5. Floyd's triangle
6. Pascal's triangle
7. Number spiral
0. Exit
Choice:
Size (1-12):
Number spiral, size 4:
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
=== Pattern Printer ===
1. Right triangle
2. Pyramid
3. Diamond
4. Hollow square
5. Floyd's triangle
6. Pascal's triangle
7. Number spiral
0. Exit
Choice:
Done.
Input
2
4
3
3
6
5
7
4
0

Testing

  • Compare with tests/expected-output.txt (pyramid 4, diamond 3, Floyd 5, spiral 4).
  • Print each pattern at size 1 — every pattern must still look correct.

Common errors

Pyramid leans to one side

The space loop must run n - i times before the stars.

Spiral has repeated or missing numbers for even sizes

Check the if (top <= bottom) and if (left <= right) guards before the bottom row and left column passes.

Possible improvements

  • Let the user choose the symbol
  • Add alphabet patterns (A, AB, ABC…)
  • Write patterns to a text file

Additional challenges

  1. 1Print a hollow diamond.
  2. 2Print the spiral counter-clockwise.
  3. 3Print Pascal's triangle up to 30 rows using unsigned long long and check where it overflows.

Built and tested it yourself? Mark it to update your dashboard.