w3resource

C++ Exercises: Print the code of a given character


ASCII/Unicode of Character

Write a C++ program to print the code (ASCII code / Unicode code etc.) of a given character.

Visual Presentation:

C++ Exercises: Print the code of a given character

Sample Solution:

C++ Code :

#include <iostream> // Including the input-output stream header file

using namespace std; // Using the standard namespace

int main() // Start of the main function
{
    char sing_ch; // Declaring a character variable

    cout << "\n\n Print code (ASCII code / Unicode code etc.) of a  given character:\n"; // Displaying the purpose of the program
    cout << "-----------------------------------------------------------------------\n";

    cout << " Input a character: "; // Prompting the user to input a character
    cin >> sing_ch; // Taking the character input from the user

    // Displaying the ASCII value of the entered character
    cout << " The ASCII value of " << sing_ch << " is: " << static_cast<int>(sing_ch) << endl;

    // Displaying the character for the entered ASCII value
    cout << " The character for the ASCII value " << static_cast<int>(sing_ch) << " is: " << static_cast<char>(static_cast<int>(sing_ch)) << endl << endl;

    return 0; // Returning 0 to indicate successful program execution
}

Sample Output:

 Print code (ASCII code / Unicode code etc.) of a  given character:    
-----------------------------------------------------------------------
 Input a character: a                                                  
 The ASCII value of a is: 97                                           
 The character for the ASCII value 97 is: a

Flowchart:

Flowchart: Print the code of a given character

For more Practice: Solve these Related Problems:

  • Write a C++ program to print the ASCII value of an input character and then display the corresponding character for a given ASCII code.
  • Write a C++ program that reads a character, prints its ASCII code, and then converts the code back to the character using casting.
  • Write a C++ program to display the Unicode values for a range of characters and then print a formatted table of the results.
  • Write a C++ program that accepts a character input and prints both its ASCII and hexadecimal values.

Go to:


PREV : Swap Variables Without Temp.
NEXT : Centimeters to Meters and Kilometers.

C++ Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.