Program to print ASCII Value of a character

30 Mar, 2022
Given a character, we need to print its ASCII value in C/C++/Java/Python.
Examples :
Input : a 
Output : 97

Input : D
Output : 68

Here are few methods in different programming languages to print 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.PythonPython# Python program to print # ASCII Value of Character  # In c we can assign different# characters of which we want ASCII value   c = 'g'# print the ASCII value of assigned character in cprint("The ASCII value of '" + c + "' is", ord(c))Output:The ASCII value of g is 103
C code: We use format specifier here to give numeric value of character. Here %d is used to convert character to its ASCII value.CC// C program to print// ASCII Value of Character#include <stdio.h>int main(){    char c = 'k';      // %d displays the integer value of a character    // %c displays the actual character    printf("The ASCII value of %c is %d", c, c);    return 0;}Output:The ASCII value of k is 107
C++ code: Here int() is used to convert character to its ASCII value.CPPCPP// CPP program to print// ASCII Value of Character#include <iostream>using namespace std;int main(){    char c = 'A';    cout << "The ASCII value of " << c << " is " << int(c);    return 0;}Output: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.JavaJava// Java program to print// ASCII Value of Characterpublic class AsciiValue {      public static void main(String[] args)    {          char c = 'e';        int ascii = c;        System.out.println("The ASCII value of " + c + " is: " + ascii);    }}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.CSHARPCSHARP// C# program to print// ASCII Value of Characterusing System;  public class AsciiValue {    public static void Main()    {          char c = 'e';        int ascii = c;        Console.Write("The ASCII value of " +                         c + " is: " + ascii);    }}  // This code is contributed // by nitin mittalOutput:The ASCII value of e is 101

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.PythonPython# Python program to print # ASCII Value of Character  # In c we can assign different# characters of which we want ASCII value   c = 'g'# print the ASCII value of assigned character in cprint("The ASCII value of '" + c + "' is", ord(c))Output:The ASCII value of g is 103

Python
# Python program to print # ASCII Value of Character  # In c we can assign different# characters of which we want ASCII value   c = 'g'# print the ASCII value of assigned character in cprint("The ASCII value of '" + c + "' is", ord(c))
The ASCII value of g is 103

C code: We use format specifier here to give numeric value of character. Here %d is used to convert character to its ASCII value.CC// C program to print// ASCII Value of Character#include <stdio.h>int main(){    char c = 'k';      // %d displays the integer value of a character    // %c displays the actual character    printf("The ASCII value of %c is %d", c, c);    return 0;}Output:The ASCII value of k is 107

C
// C program to print// ASCII Value of Character#include <stdio.h>int main(){    char c = 'k';      // %d displays the integer value of a character    // %c displays the actual character    printf("The ASCII value of %c is %d", c, c);    return 0;}
The ASCII value of k is 107

C++ code: Here int() is used to convert character to its ASCII value.CPPCPP// CPP program to print// ASCII Value of Character#include <iostream>using namespace std;int main(){    char c = 'A';    cout << "The ASCII value of " << c << " is " << int(c);    return 0;}Output:The ASCII value of A is 65

CPP
// CPP program to print// ASCII Value of Character#include <iostream>using namespace std;int main(){    char c = 'A';    cout << "The ASCII value of " << c << " is " << int(c);    return 0;}
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.JavaJava// Java program to print// ASCII Value of Characterpublic class AsciiValue {      public static void main(String[] args)    {          char c = 'e';        int ascii = c;        System.out.println("The ASCII value of " + c + " is: " + ascii);    }}
Java
// Java program to print// ASCII Value of Characterpublic class AsciiValue {      public static void main(String[] args)    {          char c = 'e';        int ascii = c;        System.out.println("The ASCII value of " + c + " is: " + ascii);    }}
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.CSHARPCSHARP// C# program to print// ASCII Value of Characterusing System;  public class AsciiValue {    public static void Main()    {          char c = 'e';        int ascii = c;        Console.Write("The ASCII value of " +                         c + " is: " + ascii);    }}  // This code is contributed // by nitin mittalOutput:The ASCII value of e is 101

CSHARP
// C# program to print// ASCII Value of Characterusing System;  public class AsciiValue {    public static void Main()    {          char c = 'e';        int ascii = c;        Console.Write("The ASCII value of " +                         c + " is: " + ascii);    }}  // This code is contributed // by nitin mittal
Output:
The ASCII value of e is 101

Here is a method to print the ASCII value of the characters in a string using python:
Python3
print("Enter a String: ", end="")text = input()textlength = len(text)for char in text:    ascii = ord(char)    print(char, "\t", ascii)
Input:
Enter a String: Aditya Trivedi
Output:
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
nitin mittal
saurabh1990aror
adityatri
ASCII
C Programs
C++ Programs
Java Programs
Python Programs
School Programming
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
C Program to read contents of Whole File
C++ Program to check Prime Number
Producer Consumer Problem in C
C Program to Swap two Numbers
Set, Clear and Toggle a given bit of a number in C
Sorting a Map by value in C++ STL
Shallow Copy and Deep Copy in C++
C++ program for hashing with chaining
C++ Program to check if a given String is Palindrome or not
delete keyword in C++