Java program to print all Lowercase Alphabets

In Java, to print all lowercase alphabets we can use loop concepts such as while loop or for loop. If the input alphabet is a, then the output will be a, b, c…. and z. If the input alphabet (character) is d, then then output will be d, e, f, g, ….. till z. Here, the user has the flexibility to specify where to start printing the alphabets.

In this article we will learn how print all lowercase alphabets through an example, detailed logic and program explanation for better understanding.

Required Knowledge

Problem statement

We have to write Java program to print all lowercase alphabets. The program should take any alphabetical character as a input. And it should print all the lowercase alphabets from user entered alphabet to z.

Let’s see an example to understand it.

Example:

For input alphabet a, Output should be a – z.

Input alphabet: a

Output:
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

Here for the input a, Output is all the lowercase alphabets from a to z.

Similarly, for user input d, output should be d-z.

Input alphabet: d

Output:
d e f g h i j k l m n o p q r s t u v w x y z

Logic to print all lowercase alphabets

  • Take an input character (ch) from the user using next().charAt(0) method.
  • It uses an if-else statement to check whether the input character is uppercase using the Character.isUpperCase(ch).
  • If the character (ch) is uppercase, it prints a message asking the user to enter a lowercase character. Else, if the character (ch) is lowercase or any other character (non-alphabetic), it proceeds to print the lowercase alphabets.
  • Enter a loop (while or for loop) that iterates as long as ch is between ‘a’ and ‘z’ inclusive (ch>=’a’ && ch<=’z’).
  • Inside the loop, it prints the current character ch followed by a tab (\t) to separate each character. After printing, it increments ch to move to the next character in the alphabet until ‘z’.
  • After loop completes, it closes the scanner class.

Java Program to print all Lowercase alphabets

Program 1: Using while loop

In the below program we are using while loop to print all the lowercase alphabets from a to z in Java.

import java.util.Scanner;
public class LowercaseAlphabets {
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		System.out.println("Enter an alphabet");
		char ch = s.next().charAt(0);
		if (Character.isUpperCase(ch)) {
            System.out.println("Entered character is not lowercase. Please enter lowercase character");
        } else {
            System.out.println("All Lowercase Alphabets are:");
    		  while (ch >= 'a' && ch <= 'z') {
    			  System.out.print(ch + "\t");
    			  ch++;
    		  }
        }
		s.close();
	}
}

Output:

Enter an alphabet
a
All Lowercase Alphabets 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

Java program explanation to print all lowercase alphabets

  1. The above java program starts by asking the user to input an alphabet.
  2. It reads the input character using a Scanner.
  3. It enters a while loop that continues as long as the input character is within the range of lowercase alphabets ('a' to 'z').
  4. Within the loop, it prints the current character followed by a tab (\t) to separate each alphabet.
  5. It increments the character (ch++) to move to the next consecutive lowercase alphabet.
  6. After printing all consecutive lowercase alphabets, it closes the Scanner to release system resources.

Program 2: Using for loop

In the below program we are using for loop to print all the lowercase alphabets in Java.

import java.util.Scanner;
public class LowercaseAlphabets{
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		System.out.println("Enter an alphabet");
		char ch = s.next().charAt(0);
		if (Character.isUpperCase(ch)) {
            System.out.println("Entered character is not lowercase. Please enter lowercase character");
        } else {
            System.out.println("All Lowercase Alphabets are:");
    		  for (char cha =ch; (cha >= 'a' && cha <= 'z');cha++) {
    			  System.out.print(cha + "\t"); 
    		  }
        }
		s.close();
	}
}

Output:

Enter an alphabet
r
All Lowercase Alphabets are:
r s t u v w x y z

Java program explanation to print all lowercase alphabets

  1. The java program starts by asking the user to input an alphabet.
  2. It reads the input character using a Scanner.
  3. It initializes a for loop with the loop variable cha set to the input character.
  4. The loop condition checks whether the current character cha is within the range of lowercase alphabets ('a' to 'z').
  5. Within the loop, it prints the current character followed by a tab (\t) to separate each alphabet.
  6. It increments the loop variable cha using cha++ to move to the next consecutive lowercase alphabet.
  7. After printing all consecutive lowercase alphabets, it closes the Scanner to release system resources.

Conclusion

In this article, we have leant how to print all the lowercase alphabets in Java. We have seen the logic and implemented the program using while loop and for loop. Hope this article helped you in the understanding of the program.

You can also check our another awesome tutorials:

Happy Learning!!

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.