ASCII value is nothing but represents the english character as numbers which means every letter present in english dictionary holds a number from 0 to 127. for example the ASCII value for capital or uppercase A is '65' and ASCII value for small or lowercase a is '97'.
In this article, you will learn how to write a program to find ASCII value of a character in C, C++, JAVA and PHP Language example.
C Program to find ASCII Value of a Character
//C Language program to get ASCII Value
//Taking input from user
#include<stdio.h>
void main(){
char c;
//Telling user what to enter
printf("Enter a character : ");
//Taking input from User
scanf("%c",&c);
printf("ASCII Value of %c = %d", c, c);
}
OUTPUT ::
Enter a character : a
ASCII Value of a = 97
C++ Program to find ASCII Value of a Character
//C++ Program to get the ASCII value
//Taking input from user
//Including the input/Output Stream Library of CPP
#include<iostream>
using namespace std;
//Main method of the program
int main(){
//Declaring a character type variable
char c;
//Talling user what to enter
cout << "Enter a character / Nmumber : ";
//Taking input from user
cin >> c;
//Printing the result
cout << "ASCII Vaule for " << c << " is : " << int(c);
return 0;
}
OUTPUT ::
Enter a character / Nmumber : @
ASCII Vaule for @ is : 64
JAVA Program to find ASCII Value of a Character
//Java Program to get the ASCII Value
//Taking input from user
//importing Scanner Class for taking input from user
import java.util.Scanner;
//Main Class of Program
public class Main{
//Main Method of the program
public static void main (String[] args) {
//Creating the object of Scanner Class
Scanner input = new Scanner(System.in);
//Telling user what to enter
System.out.print("Enter a Character / Number : ");
//String user input character in variale `c` of Character type
char c = input.next().charAt(0);
/*
*Printing the result { (int) will convert the character type value into
* it'e equilent integer value }
*/
System.out.println("ASCII VALUE IS : " + (int) c);
}
}
OUTPUT ::
Enter a Character / Number : a
ASCII VALUE IS : 97
PHP Program to find ASCII Value of a Character
<?php
// PHP Program to get the ASCII Value
//Using Ord Method
//Declaring and Initilizing the character
$inputVariable = 'a';
//Printing Output
echo "ASCII VALUE FOR ".$inputVariable." is ".ord($inputVariable);
OUTPUT ::
ASCII VALUE FOR a is 97