Wednesday, June 8, 2022

[Solved] How to Print Alphabets in Upper and Lower Case in Java? Example Tutorial

Hello guys, if you are learning Java and looking for basic programming exercise to build your concepts then you have come to the right place. Earlier, I have shared 15 common Java programming exercises and today, you will learn one of them. 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 like 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 like 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 like 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 in Java? Example

Here 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.

[Solved] How to Print Alphabets in Upper and Lower Case in Java? Example Tutorial


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 A B C D in both upper and lower 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



Write a program to print alphabets in JavaThat's all on how to print alphabets on the lower and upper case in Java. This is a good exercises to learn programming in Java. You will learn about for loop and different programming operators like ++ and logical operator like less than and greater than while solving this problem. You can even try to do this task in a different way, try using a different loop like while, for-each, or do-while. You can also try the following programming exercise to get some more practice.

Other Java Programming exercises to solve


Thank you for reading this article so far. If you like this Java coding problem and my solution and explanation then please tell your friends about it or share on social media. If you have any questions or feedback then please drop a note. 

4 comments:

  1. can we print mix letters like 1 uppercase and 1 lower case

    ReplyDelete
    Replies
    1. Yes you can, just add 26 on each character to print upper cases means

      System.out.printf("%s ", ch);
      System.out.printf("%s ", ch + 26);

      Though, I am not sure if adding 26 will give you the capital letter, just test it out, but idea is this find the exact difference between smallcase and uppercase and generate uppercase character on the fly.

      Delete
  2. can you tell how to display uppercase alphabets using unicode using an array ad display it in bluej terminal window.

    ReplyDelete
  3. Just store the unicode values of uppercase Alphabets in array and convert them into character before printing. Java has overloaded System.out.println() method, choose the one which is most appropriate.

    ReplyDelete

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