Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Friday, November 6, 2020

Java Program To Print ALL Characters From A to Z Using Loops

1. Overview

In this article, You'll learn how to display the english alphabetics in order in java. And also will show the example programs to print uppercase and lowercase characters.

2. Example To Print Alphabets From a to z Lowercase


First, write the simple program running a for loop with char starts from a to till z.
public class PrintLowerCaseAlphabets {

	public static void main(String[] args) {

		// first character
		char firstChar = 'a';

		// last character
		char lastChar = 'z';

		// print lowercase letters from a to z
		for (char c = firstChar; c <= lastChar; c++) {
			System.out.print(c + " ");

		}

	}

}

Output:
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 

In the above program, we ran the loop through a to z but internally it uses the ASCII codes such as from 65 to 90.

3. Example To Print Alphabets From A to Z Uppercase


With the little changes to the above program, you can print the upper case letters.

Just replace the code 'a' with upper 'A' and 'z' with upper 'Z'. Then, it uses internally ASCII codes from 97 to 122.
public class PrintUpperCaseAlphabets {

	public static void main(String[] args) {

		// first character
		char firstChar = 'A';

		// last character
		char lastChar = 'Z';

		// print uppercase letters from A to Z
		for (char c = firstChar; c <= lastChar; c++) {
			System.out.print(c + " ");

		}

	}

}

Output:
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 

4. Conclusion


in this article, you've seen the two ways to print the lower and upper case letters from a to z and A to Z in java with example programs.

Instead of running loop through characters, alternatively can run through the ASCII codes range.

Example programs are GitHub.

No comments:

Post a Comment

Please do not add any spam links in the comments section.