
Pattern Programs in C : A Techsnap Guide
Pattern programs are one of the first places where C starts feeling less like theory and more like problem-solving.
A teacher may ask you to print a star triangle. An interviewer may ask for a number pyramid. A coding test may ask for a hollow rectangle. At first, these questions look simple. Then you write the loops, run the code, and the output comes out sideways.
That is normal.
Pattern problems are not really about stars, numbers, or letters. They are about learning how nested loops work. The outer loop controls the rows. The inner loop controls what gets printed inside each row. Once you understand that relationship, most pattern questions become much easier.
This guide covers the most common number, star, pyramid, and character patterns in C. Run each one in the Techsnap compiler, change the number of rows, and watch how the output changes.
The Core Idea Behind Every Pattern
Every pattern starts with one question:
What should appear on each row?
For most patterns, you need to calculate three things:
- How many spaces should print before the pattern?
- How many characters or numbers should print?
- Does the pattern grow, shrink, or do both?
The outer loop gives you the current row number. The inner loops use that row number to print spaces, stars, numbers, or characters.
For example, in a simple triangle with five rows:
* * * * * * * * * * * * * * *Row 1 prints one star.
Row 2 prints two stars.
Row 3 prints three stars.
That means the inner loop runs from 1 to the current row number.
1. Number Patterns in C
Number patterns are a good starting point because the output makes the loop logic easy to see.
Left-Aligned Number Triangle
This pattern prints numbers from 1 up to the current row number.
For n = 5, the output looks like this:
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
#include <stdio.h> int main() { int n, i, j; printf("Enter number of rows: "); scanf("%d", &n); for (i = 1; i <= n; i++) { for (j = 1; j <= i; j++) { printf("%d ", j); } printf("\n"); } return 0; }The outer loop controls the row number using i.
The inner loop starts at 1 and ends at i. So when i is 3, the program prints 1 2 3.
Centered Number Pyramid
This pattern prints numbers in a centered pyramid. It uses one loop for spaces and another loop for numbers.
For n = 5, the output looks like this:
1 121 12321 1234321 123454321
#include <stdio.h> int main() { int n, i, j; printf("Enter number of rows: "); scanf("%d", &n); for (i = 1; i <= n; i++) { for (j = 1; j <= n - i; j++) { printf(" "); } for (j = 1; j <= i; j++) { printf("%d", j); } for (j = i - 1; j >= 1; j--) { printf("%d", j); } printf("\n"); } return 0; }The first inner loop adds spaces.
The second loop prints numbers going upward.
The third loop prints numbers going back down.
That is the basic formula for many pyramid patterns: spaces, increasing values, decreasing values.
2. Star Patterns in C
Star patterns are the most common pattern questions in college labs, beginner coding tests, and placement preparation.
The logic is the same as number patterns. The only difference is that you print * instead of a number.
Right-Angle Star Triangle
For n = 5, the output looks like this:
#include <stdio.h> int main() { int n, i, j; printf("Enter number of rows: "); scanf("%d", &n); for (i = 1; i <= n; i++) { for (j = 1; j <= i; j++) { printf("* "); } printf("\n"); } return 0; } This is one of the easiest pattern programs in C.
The number of stars is always equal to the row number.
Centered Star Pyramid
For n = 5, the output looks like this:
* *** ***** ******* *********
#include <stdio.h> int main() { int n, i, j; printf("Enter number of rows: "); scanf("%d", &n); for (i = 1; i <= n; i++) { for (j = 1; j <= n - i; j++) { printf(" "); } for (j = 1; j <= (2 * i - 1); j++) { printf("*"); } printf("\n"); } return 0; } The number of spaces reduces by one on every row.
The number of stars follows this formula:
2 × row number - 1So the rows print 1, 3, 5, 7, and 9 stars.
Star Diamond
A diamond is just two pyramids joined together.
The first half grows.
The second half shrinks.
For n = 5, the output looks like this:
* *** ***** ******* ********* ******* ***** *** *
#include <stdio.h> int main() { int n, i, j; printf("Enter number of rows: "); scanf("%d", &n); for (i = 1; i <= n; i++) { for (j = 1; j <= n - i; j++) { printf(" "); } for (j = 1; j <= (2 * i - 1); j++) { printf("*"); } printf("\n"); } for (i = n - 1; i >= 1; i--) { for (j = 1; j <= n - i; j++) { printf(" "); } for (j = 1; j <= (2 * i - 1); j++) { printf("*"); } printf("\n"); } return 0; }The second outer loop starts from n - 1 so the middle row does not print twice.
Hollow Rectangle
A hollow rectangle prints stars only on the border.
The inside area prints spaces.
For rows = 5 and columns = 7, the output looks like this:
* * * * * * * * * * * * * * * * * * * *
#include <stdio.h> int main() { int rows = 5; int columns = 7; int i, j; for (i = 1; i <= rows; i++) { for (j = 1; j <= columns; j++) { if (i == 1 || i == rows || j == 1 || j == columns) { printf("* "); } else { printf(" "); } } printf("\n"); } return 0; }The if condition checks whether the current position is on the first row, last row, first column, or last column.
If it is on the border, print a star.
Otherwise, print spaces.
3. Character Patterns in C
Character patterns use the same loop logic as number patterns.
The only difference is that you print letters.
In C, characters are stored as numeric values. That is why this expression works:
'A' + j - 1When j is 1, it prints A.
When j is 2, it prints B.
When j is 3, it prints C.
Alphabet Triangle
For n = 5, the output looks like this:
A A B A B C A B C D A B C D E
#include <stdio.h> int main() { int n, i, j; printf("Enter number of rows: "); scanf("%d", &n); for (i = 1; i <= n; i++) { for (j = 1; j <= i; j++) { printf("%c ", 'A' + j - 1); } printf("\n"); } return 0; }The outer loop controls the number of rows.
The inner loop prints letters from A up to the current row number.
How to Solve Any Pattern Problem
Do not try to memorize every pattern.
Instead, break the output into rows.
For each row, ask these questions:
- How many spaces are needed?
- How many stars, numbers, or letters are needed?
- Does the row grow or shrink?
- Does the pattern need a second half?
- Does the border need a special condition?
For example, in a centered star pyramid : Spaces = n - i, Stars = 2 × i - 1
For a right-angle triangle : Stars = i
For a hollow rectangle : Print a star only on the first row, last row, first column, or last column.
Once you write the formula for each row, the code becomes much easier to build.
Practice Pattern Programs on Techsnap
Pattern questions are one of the best ways to improve your loop logic before moving into arrays, strings, functions, and data structures.
Open the Techsnap compiler, try these programs with different row values, and change the output yourself.
Then move to Techsnap practice questions to solve timed pattern challenges, track your attempts, and find out where your loop logic needs more work.
Start small.
Print one correct row.
Then print the next one.
That is how pattern programming starts making sense.
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