Program to print ASCII Value of a character
Given a character, we need to print its ASCII value in C/C++/Java/Python.
Input : a Output : 97 Input : D Output : 68
Here are few methods in different programming languages to print the ASCII value of a given character :
- Python code using ord function :
ord(): It converts the given string of length one, returns an integer representing the Unicode code point of the character. For example, ord(‘a’) returns the integer 97.
The ASCII value of g is 103
- C code: We use format specifier here to give the numeric value of character. Here %d is used to convert character to its ASCII value.
The ASCII value of k is 107
- C++ code: Here int() is used to convert character to its ASCII value.
The ASCII value of A is 65
- Java code: Here, to find the ASCII value of c, we just assign c to an int variable ASCII. Internally, Java converts the character value to an ASCII value.
- C# code : Here, to find the ASCII value of c, we just assign c to an int variable ascii. Internally, C# converts the character value to an ASCII value.
The ASCII value of e is 101
Here is a method to print the ASCII value of the characters in a string using python:
Enter a String: Aditya Trivedi
A 65 d 100 i 105 t 116 y 121 a 97 32 T 84 r 114 i 105 v 118 e 101 d 100 i 105
Attention reader! All those who say programming isn’t for kids, just haven’t met the right mentors yet. Join the Demo Class for First Step to Coding Course, specifically designed for students of class 8 to 12.
The students will get to learn more about the world of programming in these free classes which will definitely help them in making a wise career choice in the future.