If you’re learning programming, you’ve definitely come across pattern problems in coding practice or interviews.
They look like this:
*
* *
* * *
* * * *
At first glance, they may feel like just printing stars, but solving them teaches you three powerful things:
-
How nested loops work.
-
How to think in terms of rows and columns.
-
How to break down a bigger problem into smaller sub-problems.
Let’s dive in, starting from simple patterns and working our way up to moderately complex ones.
🔹 1. The Foundation: Single Row and Column
Before doing fancy patterns, let’s start simple.
Example: Print 5 stars in a row
#include <stdio.h>int main() { for(int i = 0; i < 5; i++) { printf("* "); } return 0;}
🔹 2. Right-Angled Triangle (Basic Nested Loop)
Now let’s use nested loops.
#include <stdio.h>int main() { int n = 5; for(int i = 1; i <= n; i++) { for(int j = 1; j <= i; j++) { printf("* "); } printf("\n"); } return 0;}
🔹 3. Inverted Triangle
#include <stdio.h>int main() { int n = 5; for(int i = n; i >= 1; i--) { for(int j = 1; j <= i; j++) { printf("* "); } printf("\n"); } return 0;}
🔹 4. Number Triangle
Patterns aren’t just about stars *. You can print numbers too.
#include <stdio.h>int main() { int n = 5; for(int i = 1; i <= n; i++) { for(int j = 1; j <= i; j++) { printf("%d ", j); } printf("\n"); } return 0;}
🔹 5. Pyramid Shape (Moderately Complex)
Now let’s add spaces to center-align stars.
#include <stdio.h>int main() { int n = 5; for(int i = 1; i <= n; i++) { // Print spaces for(int j = 1; j <= n - i; j++) { printf(" "); } // Print stars for(int j = 1; j <= 2*i - 1; j++) { printf("* "); } printf("\n"); } return 0;}
👉 Output:
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
🔹 6. Diamond Pattern (Level Up!)
A diamond = pyramid + inverted pyramid.
#include <stdio.h>int main() { int n = 5; // Upper pyramid for(int i = 1; i <= n; i++) { for(int j = 1; j <= n - i; j++) { printf(" "); } for(int j = 1; j <= 2*i - 1; j++) { printf("*"); } printf("\n"); } // Lower inverted pyramid for(int i = n - 1; i >= 1; i--) { for(int j = 1; j <= n - i; j++) { printf(" "); } for(int j = 1; j <= 2*i - 1; j++) { printf("*"); } printf("\n"); } return 0;}
👉 Output:
*
***
*****
*******
*********
*******
*****
***
*
🔹 7. Hollow Square (Moderately Complex)
Adding conditions inside the loop.
#include <stdio.h>int main() { int n = 5; for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { if(i == 0 || i == n-1 || j == 0 || j == n-1) { printf("* "); } else { printf(" "); } } printf("\n"); } return 0;}
👉 Output:
* * * * *
* *
* *
* *
* * * * *
🎯 Key Takeaways
Outer loop → rows. Inner loop → columns.
-
Pattern problems are about thinking in 2D (row × column).
-
Adding conditions (like spaces, numbers, or boundaries) makes patterns more interesting.
-
Once you master these, you’ll breeze through coding challenges and campus interview questions.
⚡ Challenge for You!
Try to code this pattern by yourself (hint: combine numbers + spaces):
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
👉 Once you solve this, you’ve officially mastered moderately complex patterns with for loops in C 🎉
Post a Comment