Print an Alphabet Stairs Pattern in C

To print an alphabet stairs pattern in C, we use nested loops where the outer loop controls the number of rows and the inner loop prints the increasing sequence of alphabets. This pattern forms a stair-like structure with alphabets.


Examples of Alphabet Stairs Pattern

1. Printing Alphabet Stairs with Uppercase Letters

In this example, we will print an alphabet stairs pattern where each row starts from ‘A’ and prints increasing letters in sequence.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int i, j;
    char letter = 'A';

    // Outer loop for rows
    for (i = 1; i <= 5; i++) {
        // Inner loop for printing letters
        for (j = 1; j <= i; j++) {
            printf("%c ", letter);
        }
        letter++; // Increment letter for next row
        printf("\n");
    }

    return 0;
}

Explanation:

  1. We declare variables i and j for loops and letter to store the starting character.
  2. The outer loop (i) controls the number of rows (5 rows).
  3. The inner loop (j) prints characters up to the current row number.
  4. The letter variable starts from ‘A’ and is incremented after each row.
  5. The printf("\n") function moves to the next row.

Output:

A
B B
C C C
D D D D
E E E E E

2. Alphabet Stairs with Continuous Letters

In this example, we will print an alphabet stairs pattern where the letters continue from ‘A’ without resetting in each row.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int i, j;
    char letter = 'A';

    // Outer loop for rows
    for (i = 1; i <= 5; i++) {
        // Inner loop for printing letters
        for (j = 1; j <= i; j++) {
            printf("%c ", letter);
            letter++; // Increment letter after each print
        }
        printf("\n");
    }

    return 0;
}

Explanation:

  1. We declare variables i and j for loop control and letter for character tracking.
  2. The outer loop (i) determines the number of rows.
  3. The inner loop (j) prints letters up to the current row count.
  4. The letter variable starts at ‘A’ and increments after each print, ensuring a continuous sequence.
  5. Each row ends with a newline character (printf("\n")).

Output:

A
B C
D E F
G H I J
K L M N O

3. Reverse Alphabet Stairs Pattern

In this example, we will print a reverse alphabet stairs pattern, starting from a given letter and reducing row length.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int i, j;
    char letter = 'E';

    // Outer loop for rows
    for (i = 5; i >= 1; i--) {
        // Inner loop for printing letters
        for (j = 1; j <= i; j++) {
            printf("%c ", letter);
        }
        letter--; // Decrement letter for next row
        printf("\n");
    }

    return 0;
}

Explanation:

  1. We declare variables i and j for loop control and letter to store the initial character.
  2. The outer loop starts from 5 and decrements down to 1.
  3. The inner loop prints letters for the current row count.
  4. The letter variable starts at ‘E’ and decrements after each row.
  5. Each row ends with printf("\n") for proper formatting.

Output:

E E E E E
D D D D
C C C
B B
A

Conclusion

We explored different ways to print an alphabet stairs pattern using nested loops in C:

  1. Basic alphabet stairs pattern with uppercase letters.
  2. Continuous letter printing in a stair format.
  3. Reverse alphabet stairs pattern.