w3resource

C#: Get the ASCII value of a given character

C# Sharp Basic: Exercise-76 with Solution

Write a C# Sharp program to get the ASCII value of a given character.

Sample Solution:

C# Sharp Code:

using System;
using System.Linq;

namespace exercises
{
    class Program
    {
        // Main method where the program execution begins
        static void Main(string[] args)
        {
            // Display the ASCII values of different characters
            Console.WriteLine("Ascii value of 1 is: " + test('1'));
            Console.WriteLine("Ascii value of A is: " + test('A'));
            Console.WriteLine("Ascii value of a is: " + test('a'));
            Console.WriteLine("Ascii value of # is: " + test('#'));
        }

        // Function to obtain the ASCII value of a character
        public static int test(char ch)
        {
            return (int)ch; // Casting the character to an integer gives its ASCII value
        }
    }
}

Sample Output:

Ascii value of 1 is: 49
Ascii value of A is: 65
Ascii value of a is: 97
Ascii value of # is: 35

Flowchart:

Flowchart: C# Sharp Exercises - Get the ASCII value of a given character.

C# Sharp Code Editor:

Previous: Write a C# Sharp program which takes a positive number and return the nth odd number.
Next: Write a C# Sharp program to check whether a given word is plural or not.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.