Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

write a ‘java’ program to display characters from ‘a’ to ‘z’

 Here is a simple program that prints characters from a to Z

  1. public class DisplayAlphabets {
  2.     public static void main(String[] args) {
  3.         for (char c = 'a'; c
  4.             System.out.print(c + " ");
  5.         }
  6.     }
  7. }

This program uses a for loop to iterate through the characters from 'a' to 'z'. The loop variable "c" is initialized to 'a' and incremented by 1 on each iteration until it reaches 'z'. Inside the loop, the current character is printed using the System.out.print() method. The " " at the end of the print statement will print the character followed by a space.

Alternatively, we can use the Character.toChars(int) method to convert the int value to a char and use the for loop to iterate from 97 to 122.



This will also display the same output as the first example: "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".
Note that, in both examples, the alphabets will be printed in lowercase. If we want to print uppercase alphabets, you can use 'A' to 'Z' in the for loop or use 65 to 90 in the Character.toChars(int) method.

We can also use the java.util.stream.IntStream to display characters from 'a' to 'z'.
 Here is an example of how you can use IntStream to display characters from 'a' to 'z':

public class DisplayAlphabets {
    public static void main(String[] args) {
        IntStream.rangeClosed('a', 'z').mapToObj(c -> (char) c).forEach(System.out::print);
    }
}

This program uses the IntStream.rangeClosed() method to create a stream of ints from the ASCII value of 'a' to the ASCII value of 'z'. Then it uses the mapToObj() method to convert the ints to chars, and finally the forEach() method to print each character.

we can also use the java.util.stream.IntStream to display characters from 'A' to 'Z' using the same approach. Here is an example of how you can use IntStream to display characters from 'A' to 'Z':


public class DisplayAlphabets {
    public static void main(String[] args) {
        IntStream.rangeClosed('A', 'Z').mapToObj(c -> (char) c).forEach(System.out::print);
    }
}

This will print the uppercase alphabets : "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".

It's worth noting that, in all examples the alphabets will be displayed in a single line, if you want to display them in separate lines, you can modify the System.out.print() method to System.out.println().


This post first appeared on Java Tutorial - InstanceOfJava, please read the originial post: here

Share the post

write a ‘java’ program to display characters from ‘a’ to ‘z’

×

Subscribe to Java Tutorial - Instanceofjava

Get updates delivered right to your inbox!

Thank you for your subscription

×