In this guide, you will learn how to write a C program to display the characters from ‘A’ to ‘Z’ using loop.
Example 1: Program to print characters from ‘A’ to ‘Z’ using loop
In this example, we are using a for loop to print alphabets from ‘A’ to ‘Z’. We have a char
type variable ch
, which is initialized with the value ‘A’ and incremented by 1 on every loop iteration. At every loop iteration, the value of ch
is printed. The loop ends when the variable ch prints ‘Z’.
#include <stdio.h>
int main() {
char ch;
//Print characters from 'A' to 'Z'
for (ch = 'A'; ch <= 'Z'; ++ch)
//there is a whitespace after %c so that the
//characters have spaces in between.
printf("%c ", ch);
return 0;
}
Output:
Example 2: Print A to Z in lowercase: ‘a’ to ‘z’
Similarly, you can print ‘A’ to ‘Z’ in lowercase. The logic is same as first example. Here ch
is initialized with value ‘a’ and the loop ends when the condition ch <= 'z'
meets.
#include <stdio.h>
int main() {
char ch;
//Print characters from 'a' to 'z'
for (ch = 'a'; ch <= 'z'; ++ch)
printf("%c ", ch);
return 0;
}
Output: