If you’re a beginner learning C programming or preparing for coding interviews, practicing pattern printing problems is an excellent way to strengthen your understanding of loops. In this post, we’ll walk through how to print the following number patterns using nested loops in C.
Problem Statement
Write a C program to print the following four number patterns:
Pattern (i):
1
12
123
1234
12345
Pattern (ii):
12345
1234
123
12
1
Pattern (iii):
55555
4444
333
22
1
Pattern (iv):
1
22
333
4444
55555
C Code to Print All Four Patterns
#include <stdio.h>
int main() {
int i, j;
// Pattern i
printf("Pattern (i):\n");
for(i = 1; i <= 5; i++) {
for(j = 1; j <= i; j++) {
printf("%d", j);
}
printf("\n");
}
// Pattern ii
printf("\nPattern (ii):\n");
for(i = 5; i >= 1; i--) {
for(j = 1; j <= i; j++) {
printf("%d", j);
}
printf("\n");
}
// Pattern iii
printf("\nPattern (iii):\n");
for(i = 5; i >= 1; i--) {
for(j = 1; j <= i; j++) {
printf("%d", i);
}
printf("\n");
}
// Pattern iv
printf("\nPattern (iv):\n");
for(i = 1; i <= 5; i++) {
for(j = 1; j <= i; j++) {
printf("%d", i);
}
printf("\n");
}
return 0;
}
Explanation
Pattern (i):
Increments the number and the length of the line. Both loop counters increase.
Pattern (ii):
Starts from 5 and decreases both the length and max number of the line.
Pattern (iii):
Line numbers decrease from 5 to 1, and the same number is repeated.
Pattern (iv):
Line numbers increase from 1 to 5, with the same number repeated in each line.
Output
Pattern (i):
1
12
123
1234
12345
Pattern (ii):
12345
1234
123
12
1
Pattern (iii):
55555
4444
333
22
1
Pattern (iv):
1
22
333
4444
55555
Why Practice Number Patterns in C?
- Enhances understanding of nested loops
- Builds strong logic for competitive programming
- Great for technical interviews and coding tests
Conclusion
These simple yet powerful C number pattern programs are a must-practice for every programming student. Make sure you tweak the logic and try new variations to solidify your loop concepts.