Java Program to Print Alphabets in Upper and Lower Case

One of the textbook exercises to get started with any programming language is writing a program to print alphabets in both upper and lower case. This program allows you to explore the String class in Java with the toUpperCase() and toLowerCase() method but normally when you start, it's asked to do this without any API methods. This kind of exercise actually improves your understanding of programming language e.g. basic operators, data types like int and char. It's similar to your prime number, Fibonacci series, and factorial program exercise. 

I strongly suggest doing these textbook exercises to anyone who is just started learning a new programming language. Coming back to this program, Java has a datatype called char, which is a 2-byte unsigned integer type. It is used to store characters in Java e.g. char A = 'A'.

Similar to String literal "A" we also have the character literal 'A' which is letters enclosed in single quotes.  There is a worth-noting difference between storing character literal in char and int data type in Java. If you store character literal on integer variable e.g. int i = 'a'; will store ASCII value of 'a' and when you print, it will print ASCII value of 'a'.



How to print alphabets in upper and lower case

Write a program to print alphabets in JavaHere is our complete Java program to print characters in upper and lower case. It's simple to program without using any API method. We have two methods printAlphabets() and printAlphabetsInUpperCase(), I have made them static method so that I can call them directly from the main method, which is a static method. Why? because you can not call a non-static method from the static context in Java.



If you look at these methods, they are the simplest, you will ever see, of course apart from HelloWorld in Java. There is a loop in each of these methods which prints the value of character in each iteration and runs until it reaches the last character of alphabets in each case.

/**
*
* Java program to print alphabets in both upper and lower case.
*
* @author http://java67.blogspot.com
*/
public class PrintAlphabetsInJava{

    public static void main(String args[]) {

        // printing alphabets in lower case i.e. 'a' to 'z'
        printAlphabets();

        // printing alphabets in upper case i.e. 'A' to 'Z'
        printAlphabetsInUpperCase();
    }

    public static void printAlphabets() {
        System.out.println("List of alphabets in lowercase :");
        for (char ch = 'a'; ch <= 'z'; ch++) {
            System.out.printf("%s ", ch);
        }
    }

    public static void printAlphabetsInUpperCase() {
        System.out.println("\nList of alphabets in upper case :");
        for (char ch = 'A'; ch <= 'Z'; ch++) {
            System.out.printf("%s ", ch);
        }
    }

}

Output
List of alphabets in lowercase :
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
List of alphabets in upper case :
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

That's all on how to print alphabets on the lower and upper case in Java. You can even try to do this task in a different way, try using a different loop e.g. while, for-each, or do-while. You can also try the following programming exercise to get some more practice.

Further Learning
Write a program to find Armstrong numbers in Java
Write a program to find the GCD of two numbers in Java
How to find maximum and minimum numbers in Array
How to check if a number is a Palindrome in Java
10 more programming exercise for beginners


No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.