2 approaches to print A to Z in java | codeyourslf

In this article you can learn how to print Alphabets i.e A to Z in java programming language with 2 different approaches. codeYourslf.
3 min read

2 approaches to print Alphabets in java

2 approaches to print A to Z in java | codeyourslf

In this article you can learn how to print Alphabets i.e A to Z in java programming language with 2 different approaches.

- Here I will discuss for both upper case alphabets and lower case alphabet.

Approach :- 1 "print upper case alphabets using char"

public class upppperCaseBychar {

  public static void main(String[] args) {

    System.out.println("The upper case alphabets using char");

    for(char i = 'A'; i <= 'Z'; i++){

      System.out.print(i+" ");

    }

  } 

Output

2 approaches to print A to Z in java | codeyourslf

Approach :- 2 "print upper case alphabets using int"

     public class upperCaseByint {

    public static void main(String[] args){

      System.out.println("The upper case alphabets using int");

      for(int i = 65; i <= 90; i++){

        //By type casting from int to char

        System.out.print((char)i+" ");

      }

    }   

Approach :- 3 "print lower case alphabets using char"

     public class lowerCaseBychar {

    public static void main(String []args){

      System.out.println("lower case alphabets using char");

      for(char i = 'a'; i <= 'z'; i++){

        System.out.print(i+" ");

      }

    }   

Output

2 approaches to print A to Z in java | codeyourslf

Approach :- 4 "print lower case alphabets using int"

     public class lowerCaseByint {

    public static void main(String[] args){

      System.out.println("lower case alphabets using int");

      for(int i = 97; i <= 122; i++){

        System.out.print((char)i+" ");

      }

    }   

Code Explanation:-

Here I will explain how both char and int methods are working.

Approach :- 1 "char method"

- Here I have started the loop from A and it will go upto Z and will print all the alphabets. For lower case also, you have to apply same concept.

Approach :- 2 "int method"

- In this approach ASCII value plays a major role.

- The ASCII value of A is 65 and Z is 90, so we have to start the loop from 65 upto 90. It will print all the upper case alphabets.

- Similarly, the ASCII value of a is 97 and z is 122. So if we start the loop from 97 to 122, it will print all the lower case alphabets.

👉 If you learn something new from this article please share with your friends.
- if you have any questions or doubt, please write it on the comment below.
`

You may like these posts

4 comments

  1. second ago
    Nice content ..
    1. second ago
      Thanks a lot 😊
  2. second ago
    Java
    1. second ago
      I will also upload different programs for other programming language, like Python, javascript etc.

      Please support 👏
Hey Please 👏, feel free to share your opinion 🌍