Write a C++ program to accept a character input from the user and display its corresponding ASCII value.
ASCII value is a numerical value for all the characters, numbers, and symbols as per the American Standard Code for Information Interchange (ASCII).
Input and Output Examples
Example 1:
- Input: A
Output: The ASCII value of A is 65.
Example 2:
- Input: #
Output: The ASCII value of # is 35.
Algorithm to Find ASCII Value of a character
- Start the program.
- Prompt the user to enter a character.
- Read the character input.
- Convert the character to its ASCII value using its integer representation.
- Display the ASCII value.
- End the program.
Below is the C++ code to find the ASCII value of a character:
cpp
#include <iostream>
using namespace std;
int main() {
// Declare a variable to store the character
char ch;
// Prompt the user to input a character
cout << "Enter a character: ";
cin >> ch;
// Convert the character to its ASCII value
int asciiValue = int(ch);
// Display the ASCII value
cout << "The ASCII value of " << ch << " is " << asciiValue << "." << endl;
// Ending the program
return 0;
}
Testing the Program with Different Input Values
- Input: 7
Output: The ASCII value of 7 is 55. - Input: x
Output: The ASCII value of x is 120.