🟢 Beginner · Lesson 16
Pattern Programs
Understanding Pattern Programs
Pattern programs are a classic way to practice nested loops. The key insight is:
- Outer loop → controls the number of rows
- Inner loop → controls the number of columns (characters per row)
- The relationship between the row number and column count determines the shape
💡 Strategy to Solve Any Pattern
- Look at the pattern — how many rows?
- In each row, what's printed and how many times?
- Find the relationship between row number (i) and column count (j)
- Write outer loop for rows, inner loop(s) for columns
Star Patterns
Pattern 1: Right Triangle (Increasing)
C Language
#include <stdio.h> int main() { int n = 5; for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) // j goes from 1 to i printf("* "); printf("\n"); } return 0; }
*
* *
* * *
* * * *
* * * * *
Pattern 2: Inverted Right Triangle
C Language
int n = 5; for (int i = n; i >= 1; i--) { // i starts from n, counts down for (int j = 1; j <= i; j++) printf("* "); printf("\n"); }
* * * * *
* * * *
* * *
* *
*
Pattern 3: Square Pattern
C Language
int n = 4; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) // Always n columns printf("* "); printf("\n"); }
* * * *
* * * *
* * * *
* * * *
Pattern 4: Hollow Rectangle
C Language
int rows = 4, cols = 6; for (int i = 1; i <= rows; i++) { for (int j = 1; j <= cols; j++) { if (i == 1 || i == rows || j == 1 || j == cols) printf("* "); // Border only else printf(" "); // Interior spaces } printf("\n"); }
* * * * * *
* *
* *
* * * * * *
Number Patterns
Pattern 5: Floyd's Triangle
C Language
int n = 5, num = 1; for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) printf("%3d", num++); // num increases continuously printf("\n"); }
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Pattern 6: Number Triangle (1 to i)
C Language
for (int i = 1; i <= 5; i++) { for (int j = 1; j <= i; j++) printf("%d ", j); printf("\n"); }
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Pattern 7: Same Number Each Row
C Language
for (int i = 1; i <= 5; i++) { for (int j = 1; j <= i; j++) printf("%d ", i); // Print row number i times printf("\n"); }
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Alphabet Patterns
Pattern 8: Alphabet Triangle
C Language
for (int i = 1; i <= 5; i++) { for (int j = 0; j < i; j++) printf("%c ", (char)('A' + j)); // A B C D E printf("\n"); }
A
A B
A B C
A B C D
A B C D E
Pyramid Patterns (Centered)
Pattern 9: Centered Star Pyramid
C Language
#include <stdio.h> int main() { int n = 5; for (int i = 1; i <= n; i++) { for (int s = 1; s <= n - i; s++) // Leading spaces printf(" "); for (int j = 1; j <= 2*i - 1; j++) // Stars: 2i-1 printf("*"); printf("\n"); } return 0; }
*
***
*****
*******
*********
Pattern 10: Diamond Pattern
C Language
#include <stdio.h> int main() { int n = 4; // Upper half for (int i = 1; i <= n; i++) { for (int s = 1; s <= n - i; s++) printf(" "); for (int j = 1; j <= 2*i - 1; j++) printf("*"); printf("\n"); } // Lower half for (int i = n - 1; i >= 1; i--) { for (int s = 1; s <= n - i; s++) printf(" "); for (int j = 1; j <= 2*i - 1; j++) printf("*"); printf("\n"); } return 0; }
*
***
*****
*******
*****
***
*
Practice Summary
You've learned 10 patterns. Try these on your own:
| # | Pattern Name | Technique |
|---|---|---|
| 1 | Right Triangle ★ | j from 1 to i |
| 2 | Inverted Triangle ★ | i from n down to 1 |
| 3 | Square ★ | j always from 1 to n |
| 4 | Hollow Rectangle ★★ | border condition |
| 5 | Floyd's Triangle ★★ | continuous counter num++ |
| 6 | Number 1-to-i ★ | print j |
| 7 | Same Number Each Row ★ | print i |
| 8 | Alphabet Triangle ★★ | 'A' + j |
| 9 | Centered Pyramid ★★ | spaces + 2i-1 stars |
| 10 | Diamond ★★★ | pyramid + inverted |
🏋️ More Practice Patterns
Try: (1) Right-aligned triangle (spaces on left) (2) Pascal's triangle (3) Butterfly pattern (4) X pattern (5) Checkerboard pattern (alternating * and space)
Frequently Asked Questions
How do you print patterns in C?
Patterns are printed using nested loops: an outer loop controls the rows and one or more inner loops control what is printed in each row. By adjusting how many stars, numbers or spaces the inner loop prints per row, you can build triangles, pyramids and many other shapes.
Why are nested loops used for pattern programs?
Patterns have two dimensions — rows and the items within each row — so you need two levels of looping. The outer loop moves down the rows while the inner loop fills each row, which naturally maps to the grid-like shape of most patterns.
How do you print a pyramid pattern in C?
A pyramid needs each row to print some leading spaces followed by stars, with the spaces decreasing and the stars increasing as you go down. You use one inner loop for the spaces and another for the stars, both based on the current row number.
What is the logic behind number patterns in C?
Number patterns work like star patterns but print numbers based on the row or column index instead of a fixed character. For example printing the value of the inner loop counter gives rows like 1, 12, 123, since each row prints one more number than the last.
Are pattern programs useful for learning C?
Yes. Pattern programs are a favourite practice exercise because they force you to think carefully about nested loops, counters and conditions. Mastering them builds strong control-flow skills that carry over to arrays, matrices and many other problems.
💻 Live Code Editor
This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.