C program to print all alphabets from a to z using while loop

Write a C program to print all alphabets from a to z using while loop. How to display alphabets from a to z using while loop in C programming.

Example:
Output alphabets: a, b, c, d, e ...

Also check this program using for loop - C program to print all alphabets from a to z using for loop.

Required knowledge

Basic C programming, While loop

Program to print alphabets

/**
 * C program to print all alphabets using while loop
 */

#include <stdio.h>

int main()
{
    char ch = 'a';

    printf("Alphabets from a - z are: \n");
    while(ch<='z')
    {
        printf("%c\n", ch);
        ch++;
    }

    return 0;
} 

Note: If you want to print alphabets in uppercase you just need to replace the lower-case assignment and conditional checks statements in loop which is ch='A' and ch<='Z'.
Characters in C are internally represented as an integer value known as ASCII value.
ASCII value of a = 97
ASCII value of z = 122
Hence we can also write the above program using ASCII values.

Program to display alphabets using ASCII value

/**
 * C program to display all alphabets using while loop
 */

#include <stdio.h>

int main()
{
    int ch = 97;

    printf("Alphabets from a - z are: \n");
    while(ch<=122)
    {
        printf("%c\n", ch);
        ch++;
    }

    return 0;
} 


Output
X
_
Alphabets from a - z are:
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z


Happy coding ;)


Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

No comments:

Post a Comment