Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA Tutorial
  • Data Structures
  • Algorithms
  • Array
  • Strings
  • Linked List
  • Stack
  • Queue
  • Tree
  • Graph
  • Searching
  • Sorting
  • Recursion
  • Dynamic Programming
  • Binary Tree
  • Binary Search Tree
  • Heap
  • Hashing
  • Divide & Conquer
  • Mathematical
  • Geometric
  • Bitwise
  • Greedy
  • Backtracking
  • Branch and Bound
  • Matrix
  • Pattern Searching
  • Randomized
Open In App
Next Article:
Print given sentence into its equivalent ASCII form
Next article icon

Program to print ASCII Value of a character

Last Updated : 08 Feb, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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 : 

  1. 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. 

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 c
print("The ASCII value of '" + c + "' is", ord(c))
 
 
Output
("The ASCII value of 'g' is", 103) 

Time Complexity: O(1)
Auxiliary Space: O(1)

  1. C code: We use format specifier here to give numeric value of character. Here %d is used to convert character to its ASCII value. 

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;
}
 
 
Output
The ASCII value of k is 107 

Time Complexity: O(1)
Auxiliary Space: O(1)

  1. C++ code: Here int() is used to convert a character to its ASCII value. 

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;
}
 
 
Output
The ASCII value of A is 65 

Time Complexity: O(1)
Auxiliary Space: O(1)

  1. 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. 

Java




// Java program to print
// ASCII Value of Character
public class AsciiValue {
 
    public static void main(String[] args)
    {
 
        char c = 'e';
        int ascii = c;
        System.out.println("The ASCII value of " + c + " is: " + ascii);
    }
}
 
 
Output
The ASCII value of e is: 101 

Time Complexity: O(1) // since no loop is used the algorithm takes up constant time to perform the operations
Auxiliary Space: O(1) // since no extra array is used so the space taken by the algorithm is constant

  1. 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. 

CSHARP




// C# program to print
// ASCII Value of Character
using 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 

Time Complexity: O(1)
Auxiliary Space: O(1)

  1. JavaScript code: Here, to find the ASCII value of the character, the charCodeAt() method is used to get the ASCII value of the character at a specific index in a string.

Javascript




// JavaScript program to print
// ASCII Value of Character
 
console.log("The ASCII value of " + 'A' + " is " + 'A'.charCodeAt(0));
 
// This Code is Contributed by Prasad Kandekar(prasad264)
 
 

Output:

The ASCII value of A is 65

Time Complexity: O(1)
Auxiliary Space: O(1)

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

Time Complexity: O(N), where N is the length of the input string.
Auxiliary Space: O(1)



Next Article
Print given sentence into its equivalent ASCII form

A

anuragrawat1
Improve
Article Tags :
  • Competitive Programming
  • ASCII
  • Basic Coding Problems

Similar Reads

  • What is ASCII - A Complete Guide to Generating ASCII Code
    The American Standard Code for Information Interchange, or ASCII, is a character encoding standard that has been a foundational element in computing for decades. It plays a crucial role in representing text and control characters in digital form. Historical BackgroundASCII has a rich history, dating
    13 min read
  • ASCII Values Alphabets ( A-Z, a-z & Special Character Table )
    ASCII (American Standard Code for Information Interchange) is a standard character encoding used in telecommunication. The ASCII pronounced 'ask-ee', is strictly a seven-bit code based on the English alphabet. ASCII codes are used to represent alphanumeric data. The code was first published as a sta
    7 min read
  • ASCII Vs UNICODE
    Overview :Unicode and ASCII are the most popular character encoding standards that are currently being used all over the world. Unicode is the universal character encoding used to process, store and facilitate the interchange of text data in any language while ASCII is used for the representation of
    3 min read
  • ASCII Value of a Character in C
    In this article, we will discuss about the ASCII values that are bit numbers used to represent the character in the C programming language. We will also discuss why the ASCII values are needed and how to find the ASCII value of a given character in a C program. Table of Content What is ASCII Value o
    4 min read
  • ascii() in Python
    Python ascii() function returns a string containing a printable representation of an object and escapes the non-ASCII characters in the string using \x, \u or \U escapes. It's a built-in function that takes one argument and returns a string that represents the object using only ASCII characters. Exa
    3 min read
  • Program to Print ASCII Conversion

    • Program to print ASCII Value of all digits of a given number
      Given an integer N, the task is to print the ASCII value of all digits of N. Examples: Input: N = 8Output: 8 (56)Explanation:ASCII value of 8 is 56 Input: N = 240Output:2 (50)4 (52)0 (48) Approach: Using the ASCII table shown below, the ASCII value of all the digits of N can be printed: DigitASCII V
      5 min read

    • Program to print ASCII Value of a character
      Given a character, we need to print its ASCII value in C/C++/Java/Python. Examples : Input : a Output : 97 Input : DOutput : 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 o
      4 min read

    • Print given sentence into its equivalent ASCII form
      Given a string containing words forming a sentence (belonging to the English language). The task is to output the equivalent ASCII sentence of the input sentence. ASCII (American Standard Code for Information Interchange) form of a sentence is the conversion of each of the characters of the input st
      4 min read

    • Count and Print the alphabets having ASCII value not in the range [l, r]
      Given a string str, the task is to count the number of alphabets having ASCII values, not in the range [l, r]. Examples: Input: str = "geeksforgeeks", l = 102, r = 111Output: Count = 7Characters - e, s, r have ASCII values not in the range [102, 111]. Input: str = "GeEkS", l = 80, r = 111Output: Cou
      6 min read

    • Print each word in a sentence with their corresponding average of ASCII values
      Given a sentence, the task is to find the average of ASCII values of each word in the sentence and print it with the word. Examples: Input: sentence = "Learning a string algorithm"Output:Learning - 102a - 97string - 110algorithm - 107 Approach: Take an empty string and start traversing the sentence
      6 min read

    Program for Binary to ASCII Conversion

    • Python program to convert binary to ASCII
      In this article, we are going to see the conversion of Binary to ASCII in the Python programming language. There are multiple approaches by which this conversion can be performed that are illustrated below: Method 1: By using binascii module Binascii helps convert between binary and various ASCII-en
      3 min read

    • Program to convert given Binary to its equivalent ASCII character string
      Given a binary string str, the task is to find its equivalent ASCII (American Standard Code for Information Interchange) character string. Examples: Input: str = "0110000101100010"Output: abExplanation: Dividing str into set of 8 bits as follows: 01100001 = 97, ASCII value of 97 is 'a'.01100010 = 98
      9 min read

    Problem Related to ASCCI

    • Program to Convert ASCII to Unicode
      In this article, we will learn about different character encoding techniques which are ASCII (American Standard Code for Information Interchange) and Unicode (Universal Coded Character Set), and the conversion of ASCII to Unicode. Table of Content What is ASCII Characters?What is ASCII Table?What is
      4 min read

    • Program to Convert Unicode to ASCII
      Given a Unicode number, the task is to convert this into an ASCII (American Standard Code for Information Interchange) number. ASCII numberASCII is a character encoding standard used in communication systems and computers. It uses 7-bit encoding to encode 128 different characters 0-127. These values
      4 min read

    • Program to implement ASCII lookup table
      ASCII stands for American Standard Code for Information Interchange. Computers can only understand numbers, so an ASCII code is the numerical representation of a character such as ‘a’ or ‘@’ or an action of some sort. ASCII lookup table is a tabular representation of corresponding values associated
      7 min read

    • Program to find the product of ASCII values of characters in a string
      Given a string str. The task is to find the product of ASCII values of characters in the string. Examples: Input: str = "IS"Output: 605973 * 83 = 6059 Input: str = "GfG"Output: 514182 The idea is to start with iterating through characters of the string and multiply their ASCII values to a variable n
      4 min read

    • How to convert character to ASCII code using JavaScript ?
      The purpose of this article is to get the ASCII code of any character by using JavaScript charCodeAt() method. This method is used to return the number indicating the Unicode value of the character at the specified index. Syntax: string.charCodeAt(index); Example: Below code illustrates that they ca
      1 min read

    • Converting an Integer to ASCII Characters in Python
      In Python, working with integers and characters is a common task, and there are various methods to convert an integer to ASCII characters. ASCII (American Standard Code for Information Interchange) is a character encoding standard that represents text in computers. In this article, we will explore s
      2 min read

    • Check if a string contains uppercase, lowercase, special characters and numeric values
      Given string str of length N, the task is to check whether the given string contains uppercase alphabets, lowercase alphabets, special characters, and numeric values or not. If the string contains all of them, then print "Yes". Otherwise, print "No". Examples: Input: str = "#GeeksForGeeks123@" Outpu
      5 min read

    • How to remove all non-alphanumeric characters from a string in Java
      Given string str, the task is to remove all non-alphanumeric characters from it and print the modified it. Examples: Input: @!Geeks-for'Geeks,123 Output: GeeksforGeeks123 Explanation: at symbol(@), exclamation point(!), dash(-), apostrophes('), and commas(, ) are removed.Input: Geeks_for$ Geeks?{}[]
      3 min read

    • Convert the ASCII value sentence to its equivalent string
      Given a string str which represents the ASCII (American Standard Code for Information Interchange) Sentence, the task is to convert this string into its equivalent character sequence. Examples: Input: str = "71101101107115" Output: Geeks 71, 101, 101, 107 are 115 are the unicode values of the charac
      5 min read

    • Check whether the given character is in upper case, lower case or non alphabetic character
      Given a character, the task is to check whether the given character is in upper case, lower case, or non-alphabetic character Examples: Input: ch = 'A'Output: A is an UpperCase characterInput: ch = 'a'Output: a is an LowerCase characterInput: ch = '0'Output: 0 is not an alphabetic characterApproach:
      11 min read

    • Average of ASCII values of characters of a given string
      Given a string, the task is to find the average of ASCII values of characters in the string. Examples: Input: str = "for"Output: 109'f'= 102, 'o' = 111, 'r' = 114(102 + 111 + 114)/3 = 109 Input: str = "geeks" Output: 105 Source: Microsoft Internship Experience Approach: Start iterating through chara
      5 min read

    • Sums of ASCII values of each word in a sentence
      We are given a sentence of English language(can also contain digits), we need to compute and print the sum of ASCII values of characters of each word in that sentence. Examples: Input : GeeksforGeeks, a computer science portal for geeksOutput : Sentence representation as sum of ASCII each character
      7 min read

    • Program to find the largest and smallest ASCII valued characters in a string
      Given a string of lowercase and uppercase characters, your task is to find the largest and smallest alphabet (according to ASCII values) in the string. Note that in ASCII, all capital letters come before all small letters. Examples : Input : sample stringOutput : Largest = t, Smallest = aInput : Gee
      6 min read

    • Count characters in a string whose ASCII values are prime
      Given a string S. The task is to count and print the number of characters in the string whose ASCII values are prime. Examples: Input: S = "geeksforgeeks" Output : 3 'g', 'e' and 'k' are the only characters whose ASCII values are prime i.e. 103, 101 and 107 respectively. Input: S = "abcdefghijklmnop
      6 min read

    • Check for ASCII String - Python
      To check if a string contains only ASCII characters, we ensure all characters fall within the ASCII range (0 to 127). This involves comparing each character's value to ensure it meets the criteria. Using str.isascii()The simplest way to do this in Python is by using the built-in str.isascii() method
      2 min read

    • Check if a String Contains Only Alphabets in Java using ASCII Values
      Given a string, now we all know that the task is to check whether a string contains only alphabets. Now we will be iterating character by character and checking the corresponding ASCII value attached to it. If not found means there is some other character other than "a-z" or "A-Z". If we traverse th
      4 min read

    • Map function and Dictionary in Python to sum ASCII values
      We are given a sentence in the English language(which can also contain digits), and we need to compute and print the sum of ASCII values of the characters of each word in that sentence. Examples: Input : GeeksforGeeks, a computer science portal for geeksOutput : Sentence representation as sum of ASC
      2 min read

    • Count of camel case characters present in a given string
      Given a string S, the task is to count the number of camel case characters present in the given string. The camel case character is defined as the number of uppercase characters in the given string. Examples: Input: S = "ckjkUUYII"Output: 5Explanation: Camel case characters present are U, U, Y, I an
      7 min read

    • Sort an array of strings in increasing order of sum of ASCII values of characters
      Given an array arr[] consisting of N strings, the task is to sort the strings in increasing order of the sum of the ASCII (American Standard Code for Information Interchange) value of their characters. Examples: Input: arr[] = {"for", "geeks", "app", "best"}Output: app for best geeksExplanation:Sum
      7 min read

    • Count pairs of characters in a string whose ASCII value difference is K
      Given string str of lower case alphabets and a non-negative integer K. The task is to find the number of pairs of characters in the given string whose ASCII value difference is exactly K. Examples: Input: str = "abcdab", K = 0 Output: 2 (a, a) and (b, b) are the only valid pairs.Input: str = "geeksf
      7 min read

    • Count of alphabets having ASCII value less than and greater than k
      Given a string, the task is to count the number of alphabets having ASCII values less than and greater than or equal to a given integer k. Examples: Input: str = "GeeksForGeeks", k = 90Output:3, 10G, F, G have ascii values less than 90.e, e, k, s, o, r, e, e, k, s have ASCII values greater than or e
      11 min read

    • Count strings having sum of ASCII values of characters equal to a Prime or Armstrong Number
      Given an array arr[] of size N containing strings, the task is to count the number of strings having the sum of ASCII (American Standard Code for Information Interchange) values of characters equal to an Armstrong Number number or a Prime Number. Examples: Input: arr[] = {"hello", "nace"}Output:Numb
      12 min read

    • Make all characters of a string same by minimum number of increments or decrements of ASCII values of characters
      Given a string S of length N, the task is to make all characters of the string the same by incrementing/decrementing the ASCII value of any character by 1 any number of times. Note: All characters must be changed to a character of the original string. Examples: Input: S = "geeks"Output: 20Explanatio
      6 min read

    • Minimize swaps of same-indexed characters to make sum of ASCII value of characters of both the strings odd
      Given two N-length strings S and T consisting of lowercase alphabets, the task is to minimize the number of swaps of the same indexed elements required to make the sum of the ASCII value of characters of both the strings odd. If it is not possible to make the sum of ASCII values odd, then print "-1"
      9 min read

    • Count the number of words having sum of ASCII values less than and greater than k
      Given a string, the task is to count the number of words whose sum of ASCII values is less than and greater than or equal to given k. Examples: Input: str = "Learn how to code", k = 400Output:Number of words having the sum of ASCII less than k = 2Number of words having the sum of ASCII greater than
      11 min read

    • Minimum cost to make the String palindrome using ASCII values
      Given an input string S, the task is to make it a palindrome and calculate the minimum cost for making it a palindrome, where you are allowed to use any one of the following operations any number of times: For replacing the current character with another character the cost will be the absolute diffe
      5 min read

    • Find frequency of each digit 0-9 by concatenating ASCII values of given string
      Given string str, the task is to find the frequency of all digits (0-9) in a string created by concatenating the ASCII values of each character of the given string str. Example: Input: str = "GeeksForGeeks"Output: 7 21 0 0 1 2 0 5 0 0Explanation: The array of ASCII values of all characters of the gi
      5 min read

    • Super ASCII String Checker | TCS CodeVita
      In the Byteland country, a string S is said to super ASCII string if and only if the count of each character in the string is equal to its ASCII value. In the Byteland country ASCII code of 'a' is 1, 'b' is 2, ..., 'z' is 26. The task is to find out whether the given string is a super ASCII string o
      8 min read

geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences