Java Program to Display Letters from A to Z

Characters from A to Z

Java programs to display uppercase letters from A to Z or lowercase letters from a to z have been shown here. Both the iterative and recursive approaches have been covered.







1. Java program & output to Display Characters from A to Z




1.1. Java program & output to Display Characters from A to Z using Iteration

Code has been copied
/*************************************
	  alphabetacoder.com
Java Program to display characters 
from A to Z or a to z using iteration
**************************************/

import java.util.Scanner;

class Main {
    public static void main(String args[]) {
        // declare instance of Scanner class
        Scanner sc = new Scanner(System.in);

        // declare variables
        int i, s;

        // take input of the number
        System.out.println("Enter 1 to display A to Z");
        System.out.println("Enter 2 to display a to z");
        System.out.print("Your choice >> ");
        s = sc.nextInt();

        // display result
        if (s == 1) {
            // display uppercase letters
            for (i = 'A'; i <= 'Z'; i++) {
                System.out.print((char) i + " ");
            }
        } else if (s == 2) {
            // display lowercase letters
            for (i = 'a'; i <= 'z'; i++) {
                System.out.print((char) i + " ");
            }
        } else
            System.out.println("Wrong choice!");
    }
}

Output


Case 1:

Enter 1 to display A to Z

Enter 2 to display a to z

Your choice >> 1

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


Case 2:

Enter 1 to display A to Z

Enter 2 to display a to z

Your choice >> 2

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


Case 3:

Enter 1 to display A to Z

Enter 2 to display a to z

Your choice >> 3

Wrong choice!




1.2. Java program & output to Display Characters from A to Z using Recursion

Code has been copied
/*************************************
	alphabetacoder.com
Java Program to display characters 
from A to Z or a to z using recursion
**************************************/

import java.util.Scanner;

class Main {
    // recursive function to print A to Z
    public static void print_uppercase(char i, char j) {
        // display current character
        System.out.print((char) i + " ");

        // exit condition
        if (i == j)
            return;
        // call function
        print_uppercase(++i, j);
    }

    // recursive function to print a to z
    public static void print_lowercase(char i, char j) {
        // display current character
        System.out.print((char) i + " ");

        // exit condition
        if (i == j)
            return;
        // call function
        print_lowercase(++i, j);
    }

    public static void main(String args[]) {
        // declare instance of Scanner class
        Scanner sc = new Scanner(System.in);

        // declare variables
        int i, s;

        // take input of the number
        System.out.println("Enter 1 to display A to Z");
        System.out.println("Enter 2 to display a to z");
        System.out.print("Your choice >> ");
        s = sc.nextInt();

        // display result
        if (s == 1) {
            // display uppercase letters
            // by calling recursive function
            print_uppercase('A', 'Z');

        } else if (s == 2) {
            // display lowercase letters
            // by calling recursive function
            print_lowercase('a', 'z');
        } else
            System.out.println("Wrong choice!");
    }
}