Java Program to print all Uppercase Alphabets

In Java, to print all uppercase 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 E, then then output will be 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 to print all uppercase alphabets through an example, detailed logic and program explanation for better understanding.

Required Knowledge

Problem statement

We have to write a Java program to print all uppercase alphabets. The program should take any alphabetical character as a input. And should print all the uppercase alphabets from the user entered input till end which is ‘Z’. This action will be done in our program. Let’s see an example,

Example 1

For input character A, Output should be A B C……Z.

Input character 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 alphabets from A to Z.

Example 2

For input character C, Output should be C D E F……Z

Input character alphabet: C

Output:

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 C, Output is all the alphabets from C to Z

Logic to print all Uppercase 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 lowercase using the Character.isLowerCase(ch).
  • If the character (ch) is lowercase, it prints a message asking the user to enter a uppercase character. Else, if the character (ch) is uppercase or any other character (non-alphabetic), it proceeds to print the uppercase 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 Uppercase Alphabets

Program 1: Using While loop

In the below program we are using while loop to print all the uppercase alphabets from A to Z in Java.

import java.util.Scanner;

public class UppercaseAlphabets{
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.println("Enter an uppercase alphabet");
        char ch = s.next().charAt(0);
        if (Character.isLowerCase(ch)) {
            System.out.println("Entered character is not uppercase. Please enter uppercase character");
        } else {
            System.out.println("All Uppercase Alphabets are:");
            while (ch >= 'A' && ch <= 'Z') {
                System.out.print(ch + "\t");
                ch++;
            }
        }
        s.close();
    }
}

Output:

Enter an alphabet
A
All Uppercase 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 uppercase 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 enters a while loop that continues as long as the input character is within the range of uppercase 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 uppercase alphabet.
  6. After printing all consecutive uppercase 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 uppercase alphabets in Java.

import java.util.Scanner;
public class UppercaseAlphabets{
	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.isLowerCase(ch)) {
            System.out.println("Entered character is not uppercase. Please enter uppercase character");
        } else {
        System.out.println("All Uppercase Alphabets are:");
    		for (char cha =ch; (cha >= 'A' && cha <= 'Z');) {
    			System.out.print(cha + "\t");
    			cha++;
    		}
        }
		s.close();
	}
}

Output:

Enter an alphabet
N
All Uppercase Alphabets are:
N O P Q R S T U V W X Y Z

Java program explanation to print all uppercase 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 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 uppercase 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 uppercase alphabet.
  7. After printing all consecutive uppercase alphabets, it closes the Scanner to release system resources.

Conclusion

In this article, we have leant how to print all the uppercase 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:

  1. Java program to print all lowercase alphabets
  2. Java program to print ASCII character with values
  3. Fibonacci series of a given number in Java
  4. Java program to find strong numbers in a range

Happy Java 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.