Understanding how to print a character pattern in C using nested loops is a common task for beginners and essential for mastering logic building in C programming. These types of pattern programs are frequently asked in coding interviews, entrance exams, and university assessments. This article will walk you through two classic character patterns using nested loops in C with complete code, explanations, and output.
C Program to Print Character Patterns Using Nested Loops
Let’s look at two problems that illustrate how to print a character pattern in C using nested loops:
Pattern (i):
AAAAA BBBB CCC DD E
Pattern (ii):
ABCDE ABCD ABC AB A
Code: Character Pattern in C Using Nested Loops
#include <stdio.h> int main() { int i, j; // Pattern (i) printf("Pattern (i):\n"); for(i = 0; i < 5; i++) { for(j = 0; j < 5 - i; j++) { printf("%c", 'A' + i); } printf("\n"); } // Pattern (ii) printf("\nPattern (ii):\n"); for(i = 5; i >= 1; i--) { for(j = 0; j < i; j++) { printf("%c", 'A' + j); } printf("\n"); } return 0; }
Output
Pattern (i): AAAAA BBBB CCC DD E Pattern (ii): ABCDE ABCD ABC AB A
Explanation of Character Pattern in C Using Nested Loops
In the first character pattern in C using nested loops, the outer loop controls the rows, and the inner loop prints the same character multiple times. The character is determined using 'A' + i
, which makes use of ASCII values.
In the second pattern, we print characters from 'A'
to a decreasing limit in each line. This pattern also uses nested loops, where the inner loop prints from 'A'
up to the current row’s character range.
Why Practice Character Pattern in C Using Nested Loops?
Practicing a character pattern in C using nested loops helps reinforce your knowledge of:
- Loop control structures
- ASCII character manipulation
- Nested loop logic for row and column control
These patterns form the base for advanced problems like pyramid patterns, diamonds, and mirrored characters.