C program to print alphabets from a to z

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

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

Also do check this program using while loop - C program to display alphabets using while loop.

Required knowledge

Basic C programming, For loop

Program to print alphabets

/**
 * C program to print all alphabets from a to z
 */

#include <stdio.h>

int main()
{
    char ch;

    printf("Alphabets from a - z are: \n");
    for( ch='a' ; ch<='z' ; ch++)
    {
        printf("%c\n", 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 values

/**
 * C program to display all alphabets from a-z using ASCII value
 */

#include <stdio.h>

int main()
{
    int i;

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

    return 0;
} 

Note: If you want to print alphabets in uppercase using ASCII values you can use ASCII values of A = 65 and Z = 90.

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).

6 comments:

  1. Hey I did it like this Is this okay ?

    #include
    main()
    {
    char X='a';
    for(X='a';X<='z';X++)
    {
    printf("%c\n", X);
    }
    system("pause");
    }

    ReplyDelete
    Replies
    1. @Gaurav yes... the code looks all OK to me. Just to say whether you have missed <stdio.h> after #include or the blogger has escaped it as HTML. If you have missed it you must add it.

      Delete
    2. The blogger must have escaped it I have kept it in the original code

      Delete
    3. Yes, the blogger has escaped the symbol. Well for this you may use &lt+; remove + sign for < and &gt+; without + sign for >

      Delete
  2. ABCBA
    AB BA
    A A
    AB BA
    ABCBA
    !!!!!.....?????

    ReplyDelete
    Replies
    1. #include <stdio.h>

      int main()
      {
      int i, j, n;

      n = 3; //Max number of rows

      for(i=n; i>=1; i--)
      {
      for(j=1; j<=i; j++)
      {
      printf("%c", 64+j);
      }
      if(i==n)
      j=j-1;
      else //Prints space if row > 1
      printf(" ");

      for(j=j-1;j>=1; j--)
      {
      printf("%c", 64+j);
      }
      printf("\n");
      }

      for(i=1; i<=n; i++)
      {
      for(j=1; j<=i; j++)
      {
      printf("%c", 64+j);
      }
      if(i==n)
      j=j-1;
      else
      printf(" ");

      for(j=j-1;j>=1; j--)
      {
      printf("%c", 64+j);
      }
      printf("\n");
      }

      return 0;
      }

      Delete