This blog is about providing theory as well as simple executable codes of different programming languages such as java, C, C++, and web programming, etc. This blog will be helpful to the IT students to learn about programming.

Tuesday, March 14, 2023

Write a program that inputs a character from keyboard and displays its corresponding ASCII value on the screen.

 here's a C++ program that does that:

#include <iostream>
using namespace std;

int main() {
    char c;

    cout << "Enter a character: ";
    cin >> c;

    cout << "The ASCII value of " << c << " is " << int(c) << endl;
    return 0;
}
Output:

Enter a character: A
The ASCII value of A is 65

Explanation:

  • We start by including the necessary headers and using the std namespace.
  • In the main function, we declare a variable c of type char, which will store the character input by the user.
  • We prompt the user to enter a character by printing the message "Enter a character: " on the console using the cout object.
  • We read the character entered by the user using the cin object and store it in the variable c.
  • We use the int() function to convert the char value of c to its corresponding ASCII value, and then we print it on the console using the cout object.
  • We return 0 to indicate that the program has executed successfully.

No comments:

Post a Comment

If you have any doubts, please let me know

Slider Widget