Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Program for triangular patterns of alphabets in C
In C programming, triangular patterns of alphabets are commonly used to demonstrate nested loops and character manipulation. This pattern starts with a full line of consecutive alphabets and removes one character from the beginning in each subsequent line.
Syntax
for (i = 1; i <= n; i++) {
for (j = i; j <= n; j++) {
printf("%c", 'A' - 1 + j);
}
printf("<br>");
}
Algorithm
- Take input n and loop i from 1 to n
- For every i, iterate j from i to n
- Print the character by adding j to 'A' and subtracting 1
- Print a newline after each row
Example: Triangular Alphabet Pattern
This example demonstrates how to create a triangular pattern where each line starts with the alphabet corresponding to the line number −
#include <stdio.h>
int pattern(int n) {
int i, j;
for (i = 1; i <= n; i++) {
for (j = i; j <= n; j++) {
printf("%c", 'A' - 1 + j);
}
printf("<br>");
}
return 0;
}
int main() {
int n = 5;
printf("Triangular pattern for n = %d:<br>", n);
pattern(n);
return 0;
}
Triangular pattern for n = 5: ABCDE BCDE CDE DE E
Example: Pattern with Different Size
Here's another example with n = 3 to show how the pattern scales −
#include <stdio.h>
void printTriangularPattern(int n) {
for (int i = 1; i <= n; i++) {
for (int j = i; j <= n; j++) {
printf("%c", 'A' + j - 1);
}
printf("<br>");
}
}
int main() {
int n = 3;
printf("Triangular pattern for n = %d:<br>", n);
printTriangularPattern(n);
return 0;
}
Triangular pattern for n = 3: ABC BC C
How It Works
- The outer loop (i) controls the number of rows
- The inner loop (j) starts from i and goes to n, creating the triangular shape
- Character calculation: 'A' + j - 1 gives the correct alphabet for position j
- Each row has one less character than the previous row
Conclusion
Triangular alphabet patterns are excellent for practicing nested loops and character arithmetic in C. The pattern demonstrates how loop indices can control both structure and content in formatted output.
Advertisements
