
Patterns in C Programming : Techsnap Guide
Pattern programs are one of the easiest ways to get comfortable with loops in C, but they are also one of the easiest places to get confused.
A beginner sees a triangle, a diamond, or a pyramid on the screen and thinks the code must be tricky. Then the real issue shows up. The logic is not hard. The hard part is learning how to break the pattern into rows, spaces, and printed characters.
That is what this guide is for.
Pattern programs are not about decoration. They are about learning how nested loops work, how rows and columns connect, and how a small change in logic changes the full shape. Once that clicks, a lot of other C problems start making more sense too.
The Core Idea Behind Every Pattern
Every pattern starts with the same question:
What should print on each row?
Most patterns can be broken into a few simple parts:
- how many rows there are,
- how many characters print in each row,
- whether the shape grows or shrinks,
- whether spaces are needed before the characters,
- whether the center or border needs special handling.
The outer loop usually controls rows. The inner loop usually controls the characters, numbers, or spaces inside each row.
For example, a simple right-angled triangle for n = 5 looks like this:
* ** *** **** *****That means row 1 prints 1 star, row 2 prints 2 stars, row 3 prints 3 stars, and so on.
1. Star Patterns in C
Star patterns are the best place to start because they make loop behavior visible right away.
Right-Angled Triangle Pattern
For n = 5, the output is:
* ** *** **** *****
#include <stdio.h> int main() { int n, i, j; printf("Enter the value of n: "); scanf("%d", &n); for (i = 1; i <= n; i++) { for (j = 1; j <= i; j++) { printf("*"); } printf("\n"); } return 0; }The outer loop controls the rows. The inner loop prints stars from 1 to i. That is why each new row gets one extra star.
Inverted Right-Angled Triangle Pattern
For n = 5, the output is:
***** **** *** ** *
#include <stdio.h> int main() { int n, i, j; printf("Enter the value of n: "); scanf("%d", &n); for (i = n; i >= 1; i--) { for (j = 1; j <= i; j++) { printf("*"); } printf("\n"); } return 0; }This pattern simply reverses the row count. The first row has the most stars, and every next row has one fewer.
Pyramid Pattern
For n = 5, the output is:
* *** ***** ******* *********
#include <stdio.h> int main() { int n, i, j, space; printf("Enter the value of n: "); scanf("%d", &n); for (i = 1; i <= n; i++) { for (space = 1; space <= n - i; space++) { printf(" "); } for (j = 1; j <= (2 * i - 1); j++) { printf("*"); } printf("\n"); } return 0; }The first inner loop prints spaces, and the second inner loop prints stars. The formula 2 * i - 1 gives the odd count of stars on each row.
Inverted Pyramid Pattern
For n = 5, the output is:
********* ******* ***** *** *
#include <stdio.h> int main() { int n, i, j, space; printf("Enter the value of n: "); scanf("%d", &n); for (i = n; i >= 1; i--) { for (space = 1; space <= n - i; space++) { printf(" "); } for (j = 1; j <= (2 * i - 1); j++) { printf("*"); } printf("\n"); } return 0; }This is just the pyramid turned upside down. As the row number decreases, the number of stars decreases too.
2. Number Patterns in C
Number patterns are useful because they make you think about what value should appear on each row instead of just printing symbols.
Floyd’s Triangle
For n = 5, the output is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <stdio.h> int main() { int i, j, num = 1; for (i = 1; i <= 5; i++) { for (j = 1; j <= i; j++) { printf("%d ", num); num++; } printf("\n"); } return 0; }Floyd’s Triangle is built using a counter variable. You start with 1 and increase it after each print.
Pascal’s Triangle
For n = 5, the output is:
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1
#include <stdio.h> int main() { int i, j, space, coef; for (i = 0; i < 5; i++) { for (space = 1; space <= 5 - i; space++) { printf(" "); } for (j = 0; j <= i; j++) { if (j == 0 || i == 0) { coef = 1; } else { coef = coef * (i - j + 1) / j; } printf("%d ", coef); } printf("\n"); } return 0; }Pascal’s Triangle is a good example of a pattern that mixes loops with math. Each number is a binomial coefficient.
3. Hollow Patterns in C
Hollow patterns are where conditions start to matter as much as loops.
Hollow Square Pattern
Hollow Right-Angled Triangle
For n = 5, the output is:
* ** * * * * *****
#include <stdio.h> int main() { int i, j; for (i = 1; i <= 5; i++) { for (j = 1; j <= i; j++) { if (i == 5 || j == 1 || j == i) { printf("*"); } else { printf(" "); } } printf("\n"); } return 0; }This pattern teaches boundary checking inside a triangle shape.
4. Character Patterns in C
Character patterns work like number patterns, except letters replace digits.
Alphabet Pyramid Pattern
For n = 5, the output is:
A ABA ABCBA ABCDCBA ABCDEDCBA
#include <stdio.h> int main() { int i, j, space; char ch; for (i = 1; i <= 5; i++) { for (space = 1; space <= 5 - i; space++) { printf(" "); } for (j = 1, ch = 'A'; j <= i; j++, ch++) { printf("%c", ch); } for (j = i - 1, ch = ch - 2; j >= 1; j--, ch--) { printf("%c", ch); } printf("\n"); } return 0; }The letters rise to the center and then fall back. The logic is the same as the number pyramid, just with characters instead of digits.
The Mental Model That Makes Patterns Easy
Do not try to memorize every pattern.
Instead, break the output into rows and think about three things:
- how many spaces come first,
- how many characters print next,
- whether the row grows, shrinks, or mirrors itself.
The uploaded reference article follows the same learning path: start with triangles, then move to pyramids, then combine halves, then add conditions for hollow shapes and character values.
Once you can solve those parts separately, the full pattern becomes much easier.
For example:
Spaces = n - i Stars = 2 * i - 1For a right triangle:
Stars = iFor a hollow square:
Print stars only on the border.That is the real skill behind pattern programming.
Practice These Patterns on Techsnap
Pattern questions are one of the best ways to build loop confidence in C.
They help you think more clearly, write cleaner nested loops, and prepare for interviews and lab exams. They also make it easier to move into arrays, strings, functions, and more advanced C topics later.
Open the Techsnap compiler, run these programs with different values of n, and watch how the output changes when you adjust the code.
Then move to Techsnap practice questions and solve timed pattern challenges.
Start with one pattern.
Make it work.
Then make it better.
Join Techsnap Creators
Share your knowledge and earn 🚀
Want to showcase your tech expertise and get rewarded for your insights? Join the Techsnap creator network!
Write insightful blogs, stay ahead of industry trends, and grow your professional brand while helping others in the community.
Ready to make an impact?

Comments