Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs

Java Program to print ASCII Value of a Character

by suresh

How to write a Java Program to print ASCII Value of a Character with example?. In Java programming, every character has its own ASCII value (ASCII value is nothing but an integer). When we assign a character to a variable, instead of storing the character itself, the ASCII value of that character stored. For example, the ASCII value for the A is 65.

Java Program to print ASCII Value of a Character Example

In this Java program, the user requested to enter any character. Next, this Java program displays the ASCII value of that particular character.

TIP: Please refer ASCII Table article to understand the list of Java ASCII Characters and their decimal, Hexadecimal, and Octal numbers.

// Java Program to print ASCII Value of a Character
import java.util.Scanner;

public class ASCIIofCharacter {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		char ch;
		sc = new Scanner(System.in);
		
		System.out.print(" Please Enter any Character : ");
		ch = sc.next().charAt(0);	
		
		//Assigning the Character to Integer value
		int num1 = ch;
		
		// You can also Type cast the Character Value to convert it into Integer value
		int num2 = (int)ch;
		
		System.out.println("\n The ASCII value of a given character " + ch + "  =  " + num1);
		System.out.println("\n The ASCII value of a given character " + ch + "  =  " + num2);

		// Type casting directly inside the system.out.println
		System.out.println("\n The ASCII value of a given character " + ch + "  =  " + (int)ch);
	}
}

OUTPUT

Java Program to print ASCII Value of a Character 1

This time we are inserting word. Though it is a word, this program accepts the first character only because of sc.next().charAt(0). It read a character at the position at index position 0.

Java Program to print ASCII Value of a Character 2

Placed Under: Java Programs

  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy