Program to find the ASCII value of a character | faceprep

Program to find the ASCII value of a character is discussed here. Given a character as input, the ASCII value of the character is displayed as output.

A character variable holds the ASCII value (an integer number between 0 and 127) rather than that character itself. That value is known as the ASCII value.

For example, consider the given character as input.

Input: S
Output: 83

Input: s
Output: 115

Algorithm to find the ASCII value of a character

  • Input the given character.
  • Find the ASCII value of the character.
  • Print the ASCII value of the character.

Program to find the ASCII value of a character is given below.

// C program to find the ASCII value of a character

#include

int main()
{
char ch;
scanf(“%c”,&ch);
printf(“ASCII value of %c is %d”,ch,ch);  // Prints the ASCII value of the input character
return 0;
}

// C++ program to find the ASCII value of a character

#include
using namespace std;

int main()
{
char ch;
cin >> ch;
cout << “ASCII value of ” << ch << ” is ” << int(ch);  // Prints the ASCII value of the input character
}

// Java program to find the ASCII value of a character

import java.util.*;
public class Main
{
public static void main(String[] args)
{
char ch;
Scanner sc = new Scanner(System.in);
ch = sc.next().charAt(0);
int ascii = (int) ch;
System.out.println(“ASCII of ” + ch + ” is ” + ascii);   // Prints the ASCII value of the input character
}
}

# Python program to find the ASCII value of a character

ch = ‘A’
print(“ASCII of”,ch,”is”,ord(ch))  // Prints the ASCII value of the input character

Output:

find the ascii value of a character

In this program, the user is asked to input a character which is stored on variable ch.

The ASCII value of that character is stored in variable ch, rather than that variable itself.

When %d format string is used, 115 (ASCII value of ‘s’) is displayed.

When %c format string is used, ‘s’ itself is displayed.