C Program to print Pattern 3 A A B A B C A B C D A B C D E
Objective
Write a C program to print a left-aligned right triangle of Characters.
Algorithm / Approach
- Run an outer loop using character variables:
i = 'A'to'E'. - Run an inner loop:
j = 'A'toi. - Inside the inner loop, print the current character
jusing the%cformat specifier. - Print a newline.
main.c
#include<stdio.h>
int main( ) {
char i,j;
for(i='A';i<='E';i++) {
for(j='A';j<=i;j++) {
printf("%c ",j);
}
printf("\n");
}
return 0;
}
Expected Output
A A B A B C A B C D A B C D E
Explanation of the Program
- In C, characters (
char) are actually just small integers under the hood, representing their ASCII values (e.g., 'A' is 65). - Because they are integers, you can use them directly in
forloops! The loop will iterate from 65 to 69, printing the corresponding letters perfectly.
Complexity
Time Complexity
O(n^2)
Space Complexity
O(1)