Java program to print alphabets

Let’s learn java program to print alphabets.

Java program to print alphabets

To begin with let’s see program to print alphabets in lowercase.

Here in this case internally we loop through 97 to 122. Because they are stored as ASCII characters in java. Here let’s learn java program to display characters from a to z using loop or for loop.

public class PrintAlphabetUsingForLoop
{
   public static void main(String[] args)
   {
      System.out.println("Printing alphabets from a to z : ");
      char alphabet;
      for(alphabet = 'a'; alphabet <= 'z'; alphabet++)
      {
         System.out.println(alphabet);
      }
   }
}

Output:

java program to print alphabets

Using while loop

Let’s learn program to print alphabets from a to z using while loop.

public class PrintAlphabetUsingWhileLoop
{
   public static void main(String[] args)
   {
      System.out.println("Printing alphabets from a to z : ");
      char alphabet = 'a';
      while(alphabet <= 'z')
      {
         System.out.println(alphabet);
         alphabet++;
      }
   }
}

Output:

Printing alphabets from a to z : 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


However, above example to print alphabets can also be used to print few alphabets, that is, from alphabet ‘a’ to alphabet ‘m’.

public class JavaPrintAlphabet
{
   public static void main(String[] args)
   {
      System.out.println("Printing alphabets from a to m: ");
      char alphabet = 'a';
      do
      {
         System.out.println(alphabet);
         alphabet++;
      } while(alphabet <= 'm');
   }
}

Output:

Printing alphabets from a to m: a b c d e f g h i j k l m


Meanwhile, we can also write java program to print alphabets from A to Z in uppercase.

public class PrintAlphabetUppercase
{
   public static void main(String[] args)
   {
      System.out.println("Printing alphabets from A to Z : ");
      char alphabet;
      for(alphabet = 'A'; alphabet <= 'Z'; alphabet++)
      {
         System.out.println(alphabet);
      }
   }
}

Output:

Printing alphabets from A to Z : 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


Using do while loop

Similarly let’s learn java program to display alphabets from a to z using do while loop.

public class PrintAlphabetUsingDoWhileLoop
{
   public static void main(String[] args)
   {
      System.out.println("Printing alphabets from a to z : "); 
      char alphabet = 'a';
      do
      {
         System.out.println(alphabet);
         alphabet++;
      } while(alphabet <= 'z');
   }
}

Output:

Printing alphabets from a to z: 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