Java Program to print the ASCII value of a character

Get a character from user and display the corresponding ASCII value

Sample Input 1:


Sample Output 1:


Sample Input 2:


Sample Output 2:


Try your Solution

Strongly recommended to Solve it on your own, Don't directly go to the solution given below.

public class Hello { public static void main(String args[]) { //Write your code here } }

Program or Solution

import java.util.*;
class Ascii
    public static void main(String args[])
       char ch;
       int ascii;
       Scanner sc=new Scanner(;
       System.out.println("Enter The Character:");;
       System.out.println("Equivalent Ascii Value:"+ascii);



Program Explanation

The Scanner class, which is part of the java.util package, is used to get user input. To use the Scanner class, create an object of the class and call any of the methods listed in the documentation for the Scanner class. The next() method, which is used to read character, are used in our example.

charat(0) returns first character, if user enters multiple character. 

The Input character  is stored in the variable ch and converted as integer using (int) which returns the ASCII value of the characters.