Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Java Program to Print the ASCII values

In this tutorial, we will learn how to print the ASCII values of the characters in Java. The ASCII is a standard used to represent characters on electronic devices. It is a 7-bit code that consists of 33 non-printable and 95 printable characters and includes letters, numbers, punctuation marks, and control characters.

But before moving forward if you are not familiar with the concept of typecasting in java, then do check the article on TypeCasting in Java.

Input: Enter the character: Z

Output: ASCII value of Z is 90

Program 1: Print the ASCII values in Java

In this program, we will learn how to print the ASCII value of a character by assigning the character to an integer data type.

Algorithm

  1. Start

  2. Create an instance of the Scanner class.

  3. Declare a character type variable.

  4. Ask the user to initialize the variable.

  5. Declare an integer type variable.

  6. Assign the character variable to the integer variable.

  7. Print the values of both variables.

  8. Stop

Below is the Java code to print ASCII values.

//Java Program to print the ASCII values 
import java.util.*;
public class Main
{
     //Driver code
     public static void main(String []args)
     {
        //Take input from the user
        //Create instance of the Scanner class 
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the character whose ASCII value you want to know ");
        char ch=sc.next().charAt(0);
        int value=ch;
        System.out.println("Character is "+ch+" and ASCII value is "+value);
     }
}


Enter the character whose ASCII value you want to know r
Character is r and ASCII value is 114

Program 2: Print the ASCII values in Java

In this program, we will learn how to print the ASCII value of a character by type-casting.

Algorithm

  1. Start

  2. Create an instance of the Scanner class.

  3. Declare a character type variable.

  4. Ask the user to initialize the variable.

  5. Declare an integer type variable.

  6. Cast the character type to integer type using (int)

  7. Assign this to the character variable.

  8. Print the values of both variables.

  9. Stop

Below is the Java code to print ASCII values.

//Java Program to print the ASCII values 
import java.util.*;
public class Main
{
     //Driver code
     public static void main(String []args)
     {
        //Take input from the user
        //Create instance of the Scanner class 
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the character whose ASCII value you want to know ");
        char ch=sc.next().charAt(0);
        int value=(int)ch;
        System.out.println("Character is "+ch+" and ASCII value is "+value);
     }
}


Enter the character whose ASCII value you want to know
Character is B and ASCII value is 66

Program 3: Print the ASCII values in Java

In this program, we will learn how to print all the ASCII values of English alphabets.

Algorithm:

  1. Start
  2. Use a for loop to iterate through all the English alphabets.
  3. Print both the characters and their ASCII values by typecasting the character type to integer type using (int).
  4. Stop

Below is the Java code to print ASCII values.

//Java Program to print the ASCII values 
public class Main
{
     //Driver code
     public static void main(String []args)
     {
        for(char ch='A';ch<='Z';ch++)
        {
            System.out.println("The ASCII value of "+ch+" is "+(int)ch);
        }
     }
}


The ASCII value of A is 65
The ASCII value of B is 66
The ASCII value of C is 67
The ASCII value of D is 68
The ASCII value of E is 69
The ASCII value of F is 70
The ASCII value of G is 71
The ASCII value of H is 72
The ASCII value of I is 73
The ASCII value of J is 74
The ASCII value of K is 75
The ASCII value of L is 76
The ASCII value of M is 77
The ASCII value of N is 78
The ASCII value of O is 79
The ASCII value of P is 80
The ASCII value of Q is 81
The ASCII value of R is 82
The ASCII value of S is 83
The ASCII value of T is 84
The ASCII value of U is 85
The ASCII value of V is 86
The ASCII value of W is 87
The ASCII value of X is 88
The ASCII value of Y is 89
The ASCII value of Z is 90