Java Program to Print Alphabets in Upper and Lower Case

One of the textbook exercise to get start 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 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 this 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 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 character literal 'A' which is letters enclosed in single quotes.  There is 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 prints 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 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 main method, which is a static method. Why? because you can not call a non-static method from static context in Java.


If you look at these methods, they are most simplest, you will ever see, of-course apart form HelloWorld in Java. Their is a loop in each of these method which print value of character in each iteration and runs until  it reaches 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 lower and upper case in Java. You can ever try to do this task in different way, try using different loop e.g. while, for-each or do-while. You can also try following programming exercise to get some more practice.

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

No comments:

Post a Comment