Java Program to Find ASCII Value of a character

Program to Calculate Average Using Arrays in Java

What are ASCII Values?

ASCII is a character encoding standard that represents text in computers, communications equipment, and other devices. It specifies a standardized number for each character in the ASCII table, which consists of 128 characters. These ASCII values represent the numerical values assigned to each character in the ASCII table, which can range from 0 to 127.

In this Article, we will write a program to find ASCII value of a Character..

ASCII Values:

ASCII values are often used in computer programming to represent and manipulate character data. They are also used in communication protocols, such as those used for transmitting text over the internet or other networks.

  • 0: null character
  • 9: tab
  • 10: line feed
  • 13: carriage return
  • 32: space
  • 48-57: digits 0-9
  • 65-90: uppercase letters A-Z
  • 97-122: lowercase letters a-z

Pseudo Codes for ASCII Values of  Characters:

To find the ASCII value of a character in Java, you can use the (int) type cast operator along with the charAt() method of the String class.

Here’s an example of how you can use these methods to find the ASCII value of a character in Java:

char c = 'A';
int ascii_value = (int) c;
System.out.println(ascii_value); 
Output:

65

We can also use this method to find the ASCII value of a special character, such as the space character or a newline character.

For Example,

char c = '\n';
int ascii_value = (int) c;
System.out.println(ascii_value); 

char c = ' ';
int ascii_value = (int) c;
System.out.println(ascii_value);  
Output:

10
32

Program to find ASCII Values of a Character :

Run
import java.util.*;

public class Main{
  public static void main(String[] args) {
    Scanner scn = new Scanner(System.in);

    System.out.print("Enter a character: ");
    
    // Read the first character of the user's input
    char c = scn.next().charAt(0);  

    // Taking the ASCII Value of input character
    int ascii_value = (int) c;
    System.out.println("The ASCII value of '" + c + "' is: " + ascii_value);
  }
}

This program first prompts the user to enter a character using the Scanner class. It then reads the first character of the user’s input using the charAt() method. Finally, it uses the (int) type cast operator to convert the character to its ASCII value and prints the result.

Here’s an example of how this program might be used:

Output:

Enter a character: A
The ASCII value of 'A' is: 65

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription