Java Program to print the ASCII value of a character
Get a character from user and display the corresponding ASCII value
Sample Input 1:
A
Sample Output 1:
65
Sample Input 2:
J
Sample Output 2:
74
Program or Solution
import java.util.*;
class Ascii
{
public static void main(String args[])
{
char ch;
int ascii;
Scanner sc=new Scanner(System.in);
System.out.println("Enter The Character:");
ch=sc.next().charAt(0);
ascii=(int)ch;
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.
Comments
Related Programs
- Java Program to Print Welcome
- Java Program to Print the given Message
- Java Program to Print the given word
- Java Program to print the given integer number
- Java Program to print the given fractional number
- Java Program to print the given fractional number in 2 digit decimal format
- Java Program to print two numbers with a space between them
- Java Program to print two numbers with a tab space between them
- Java Program to print two numbers in two lines
- Java Program to Print the Character of given ASCII Value
- println Statement Example in Java
- Literals in Java With Example
- Print and println difference with Example
- Command Line Argument Example in Java
- Implicit Type Conversion Example in Java
- Explicit Type Conversion Example in Java
- Escape Sequences and Format Specifiers Example in java
coming Soon
coming Soon