The Program in C# Program to Get ASCII value of Given Character is given below:
using System;
namespace ASCIIValue
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello Codeauri Family, enter a character here to find the ASCII Value below: ");
char inputChar = Console.ReadKey().KeyChar;
int asciiValue = (int)inputChar;
Console.WriteLine($"\n Okay, the ASCII value of '{inputChar}' is: {asciiValue}");
}
}
}
Output:
Hello Codeauri Family, enter a character here to find the ASCII Value below:
C
Okay, the ASCII value of ‘C’ is: 67
Pro-Tips💡
Here are the step by step execution of above program:
- We start by including the
System
namespace, which provides access to various types and methods used in the program, including theConsole
class for reading input from the user and writing output to the console. - We define a
Program
class, which contains theMain
method that is the entry point of the program. - In the
Main
method, we write a prompt to the console asking the user to enter a character usingConsole.WriteLine("Hello Codeauri Family, enter a character here to find the ASCII Value below: ")
. - We then read the user’s input using
char inputChar = Console.ReadKey().KeyChar;
. TheReadKey
method is used to read a single key from the console, and theKeyChar
property returns the character that corresponds to the entered key. This allows us to get the character entered by the user and store it in theinputChar
variable. - Next, we convert the
inputChar
to its ASCII value using the cast operator(int)
. The ASCII value is then stored in theasciiValue
variable. - Finally, we print the ASCII value to the console using
Console.WriteLine($"\nOkay, the ASCII value of '{inputChar}' is: {asciiValue}")
. The$
symbol is used to create a string interpolation, which allows us to embed the values ofinputChar
andasciiValue
directly into the output string.
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.