Java program to print alphabets

Let’s learn java program to print alphabets.

Java program to print alphabets

Print only alphabets in java is the most asked interview question.

To begin with let’s see java 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 program to print alphabets a to z using a loop.

// print only alphabets in java
public class PrintAlphabetsInJava
{
   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 all alphabets from a to z using while loop.

public class JavaAlphabet
{
   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


java program to print alphabets from ascii values

Now let’s learn program to print alphabets from ascii values.

To print a to z in java using ascii value we have to type cast.

Also read – variables in java

Here casting means converting variable from one type to another.

But internally java converts character value to ascii value.

In the below java program we are converting char variable to integer variable.

public class PrintAlphabetJava 
{
   public static void main(String[] args) 
   {
      char ch = 'B';
      int ascii = ch;
      int castAscii = (int) ch;   
      System.out.println("ascii value of " + ch + " is: " + ascii);
      System.out.println("ascii value of " + ch + " is: " + castAscii);
   }
}


Output:

ascii value of B is: 66
ascii value of B is: 66


However, above java example to print all 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 all alphabets from A to Z in uppercase.

public class JavaPrintAlphabet
{
   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 characters from a to z using do while loop.

public class AlphabetInJava
{
   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


java program to print alphabet “b” using stars

Let’s learn java program to print alphabet “b” using stars.

public class AlphabetUsingStars
{
   public static void main(String[] args)
   {
      for(int a = 0; a < 7; a++)
      {
         for(int b = 0; b < 5; b++)
         {
            if(b == 0 || (a % 3 == 0 && b != 4) || (a % 3 > 0 && b == 4))
            {
               System.out.print("* ");
            }
            else
            {
               System.out.print(" ");
            }
         }
         System.out.println();
      }
   }
}


Output:

java program to print alphabets