w3resource

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

C++ Basic: Exercise-49 with Solution

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

C++ Code Editor:

Previous: Write a program in C++ which swap the values of two variables not using third variable.
Next: Write a program in C++ to enter length in centimeter and convert it into meter and kilometer.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.