0
0
CProgramBeginner · 2 min read

C Program to Print Alphabet Pattern

Use nested for loops in C to print an alphabet pattern by incrementing a character variable inside the inner loop, for example: for(int i=0; i.
📋

Examples

Inputn = 3
OutputA AB ABC
Inputn = 5
OutputA AB ABC ABCD ABCDE
Inputn = 1
OutputA
🧠

How to Think About It

To print an alphabet pattern, think of rows and columns. Each row prints letters starting from 'A' up to a certain letter depending on the row number. Use one loop for rows and another nested loop for columns, printing letters by adding the column index to 'A'.
📐

Algorithm

1
Get the number of rows (n) as input
2
Start a loop from 0 to n-1 for each row
3
Inside this loop, start another loop from 0 to current row index
4
Print the character 'A' plus the inner loop index
5
After inner loop ends, print a newline to move to next row
6
Repeat until all rows are printed
💻

Code

c
#include <stdio.h>
int main() {
    int n = 5;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j <= i; j++) {
            printf("%c", 'A' + j);
        }
        printf("\n");
    }
    return 0;
}
Output
A AB ABC ABCD ABCDE
🔍

Dry Run

Let's trace the program for n=3 to see how it prints the pattern.

1

Start outer loop i=0

Inner loop j runs from 0 to 0, prints 'A' (65 + 0)

2

Print newline after row 0

Output so far: A

3

Outer loop i=1

Inner loop j runs 0 to 1, prints 'A' and 'B'

4

Print newline after row 1

Output so far: A AB

5

Outer loop i=2

Inner loop j runs 0 to 2, prints 'A', 'B', 'C'

6

Print newline after row 2

Output so far: A AB ABC

Row (i)Column (j)Printed Character
00A
10A
11B
20A
21B
22C
💡

Why This Works

Step 1: Outer loop controls rows

The outer for loop runs from 0 to n-1, each iteration prints one row of the pattern.

Step 2: Inner loop prints letters

The inner for loop runs from 0 to the current row index, printing letters starting from 'A' by adding the loop index.

Step 3: Newline after each row

After printing letters for a row, printf("\n") moves the cursor to the next line for the next row.

🔄

Alternative Approaches

Using while loops
c
#include <stdio.h>
int main() {
    int n = 5, i = 0;
    while (i < n) {
        int j = 0;
        while (j <= i) {
            printf("%c", 'A' + j);
            j++;
        }
        printf("\n");
        i++;
    }
    return 0;
}
Uses while loops instead of for loops; functionally same but slightly longer code.
Printing reversed alphabet pattern
c
#include <stdio.h>
int main() {
    int n = 5;
    for (int i = 0; i < n; i++) {
        for (int j = i; j >= 0; j--) {
            printf("%c", 'A' + j);
        }
        printf("\n");
    }
    return 0;
}
Prints letters in reverse order on each line, showing a different pattern style.

Complexity: O(n^2) time, O(1) space

Time Complexity

The nested loops run approximately n*(n+1)/2 times, which is O(n^2), because each row prints increasing letters.

Space Complexity

The program uses constant extra space, only variables for loop counters and no extra arrays.

Which Approach is Fastest?

All approaches use nested loops and have similar O(n^2) time; choice depends on readability and style preference.

ApproachTimeSpaceBest For
For loopsO(n^2)O(1)Simple and clear pattern printing
While loopsO(n^2)O(1)Alternative loop style, same performance
Reverse patternO(n^2)O(1)Different pattern style, same complexity
💡
Use character arithmetic like 'A' + index to print letters easily in C.
⚠️
Beginners often forget to print a newline after each row, causing all output to appear on one line.