C

Using A Loop, Write A C Program To Display Characters From A To Z

This guide will teach you how to write a C program that uses a loop to display the characters from ‘A’ to ‘Z.’

Example 1: Write a program that uses a loop to print characters from ‘A’ to ‘Z.’

In this example, we’re going to use a for loop to print the alphabet from ‘A’ to ‘Z.’ We have a char type variable ch that is initialized with ‘A’ and incremented by 1 on each loop iteration. The value of ch is printed after each loop iteration. When the variable ch prints ‘Z,’ the loop is over.

#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;
}

Example 2: Print A-Z in lowercase letters: ‘a’ to ‘z’

Similarly, you can print the letters ‘A’ through ‘Z’ in lowercase. The logic is the same as in the first example. In this case, ch is initialized with the value ‘a,’ and the loop ends when the condition ch = ‘z’ is met.

#include <stdio.h>
int main() {
  char ch;
  //Print characters from 'a' to 'z'
  for (ch = 'a'; ch <= 'z'; ++ch)
    printf("%c ", ch);
  return 0;
}

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button