Program to find the ASCII Values of the Character

Definition & Explanation

ASCII (American Standard Code for Information Interchange) is a character encoding standard that represents text in computers and other devices. It assigns numerical values (codes) to characters such as letters, digits, punctuation marks, and control characters. Each character is assigned a unique 7-bit code, which allows computers to understand and process text data.

The ASCII values are represented as integers ranging from 0 to 127. Here are some common ASCII characters along with their decimal values:

  • A: 65
  • a: 97
  • 0: 48
  • !: 33
  • Space: 32
  • Newline: 10

Program Logic

  • User gives an input
  • Input is stored in a char type variable say val.
  • val is converted from char to int .
  • The ASCII value of Character is Obtained
C

Method 1 :

#include <stdio.h>

int main() {
    char ch;
    
    printf("Enter a character: ");
    scanf("%c", &ch);
    
    printf("ASCII value of %c is %d\n", ch, ch);
    
    return 0;
}

Output :

C++

Method 1 :

#include <iostream>
using namespace std;

int main() {
    char ch;
    
    cout << "Enter a character: ";
    cin >> ch;
    
    cout << "ASCII value of " << ch << " is " << int(ch) << endl;
    
    return 0;
}

Output :

JAVA

Method 1 :

import java.util.Scanner;

public class AsciiValue {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        System.out.print("Enter a character: ");
        char ch = scanner.next().charAt(0);
        
        System.out.println("ASCII value of " + ch + " is " + (int) ch);
        
        scanner.close();
    }
}

Output :

Python

Method 1 :

ch='A'
asci_values= ord(ch)
print(f"Ascii values of {ch} is {asci_values}")

Output :

Ascii values of A is 65