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:
- We declare variables
i
andj
for loops andletter
to store the starting character. - The outer loop (
i
) controls the number of rows (5 rows). - The inner loop (
j
) prints characters up to the current row number. - The
letter
variable starts from ‘A’ and is incremented after each row. - 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:
- We declare variables
i
andj
for loop control andletter
for character tracking. - The outer loop (
i
) determines the number of rows. - The inner loop (
j
) prints letters up to the current row count. - The
letter
variable starts at ‘A’ and increments after each print, ensuring a continuous sequence. - 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:
- We declare variables
i
andj
for loop control andletter
to store the initial character. - The outer loop starts from 5 and decrements down to 1.
- The inner loop prints letters for the current row count.
- The
letter
variable starts at ‘E’ and decrements after each row. - 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:
- Basic alphabet stairs pattern with uppercase letters.
- Continuous letter printing in a stair format.
- Reverse alphabet stairs pattern.