ASCII stands for American Standard Code for Information Interchange. And it represents a positive unique value for every character used in a programming language.
Problem Statement
We need to
write a program
in C, C++, and Python that accepts a character from the user and returns its ASCII code value. For example, if the user enters
E
, then the output must be
69
, because 69 is the ASCII code value for uppercase E.
C Program to print the ASCII value of a character
#include <stdio.h>
int main()
{
char ch;
int ascii;
printf("Enter a Character: ");
scanf("%c", &ch);
//convert the character into corresponding ASCII code
ascii = ch;
printf("The ASCII value of character %c is %d", ch, ascii);
return 0;
}
Output
Enter a Character: V
The ASCII value of character V is 86
C++ Program to print the ASCII value of a character
#include<iostream.h>
#include< conio.h>
#include<math.h>
void main()
{
clrscr();
int ascii;
char character;
cout<<"Enter a character: ";
cin>>character;
//convert the character into corresponding ASCII code
ascii = character;
cout<<"The ASCII value for "<<character<<" is "<<ascii;
getch();
}
Output
Enter a character: (
The ASCII value for ( is 40
Python Program to print the ASCII value of a character
character = input("Enter a character: ")
print("The ASCII value for",character,"is",ord(character))
Output:
Enter a character: (
The ASCII value for ( is 40
Wrapping Up!
In C and C++ when we assign a character data identifier type to an integer data type, due to implicit type conversion the compiler converts the individual character data type to its equivalent ASCII value. But in Python, we have a dedicated function
ord()
that can convert a character to its ASCII code. There are many characters in programming languages and every character has its own ASCII value, that remains same for all the programming languages and these ASCII values include digits, lowercase letter, uppercase letters, and special symbols.
People are also reading:
- WAP in C++ & Python & sort an Array Using Bubble Sort
- WAP to find the Sum of Series 1/2+4/5+7/8+…
- Linear Search in Python and C++
- C++ and Python Code to Find The Sum of Elements Above and Below The Main Diagonal of a Matrix
- WAP in C++ & Python for the Union of Two Arrays
- How many Centimeters in a Foot through Python?
- WAP in C++ & Python to transpose a Matrix
- Rectangle & Pyramids Pattern in C++
- C Program to Extract a Portion of String
- WAP in C++ and Python to calculate 3X3 Matrix multiplication
Leave a Comment on this Post