c-sharp
  1. c-sharp-program-to-check-whether-a-character-is-a-vowel-or-consonant

C# Program to Check Whether a Character is a Vowel or Consonant

In this C# program, we will create a simple console application to determine whether a given character is a vowel or a consonant. This program provides an example of basic decision-making in C# using if-else statements.

Syntax

using System;

class Program
{
    static void Main()
    {
        // Variable declaration
        char inputChar;

        // Get user input
        Console.Write("Enter a character: ");
        inputChar = Convert.ToChar(Console.ReadLine());

        // Check if the character is a vowel or consonant
        if (Char.IsLetter(inputChar))
        {
            if ("aeiouAEIOU".Contains(inputChar))
            {
                Console.WriteLine($"{inputChar} is a vowel.");
            }
            else
            {
                Console.WriteLine($"{inputChar} is a consonant.");
            }
        }
        else
        {
            Console.WriteLine("Invalid input. Please enter a valid alphabet character.");
        }
    }
}

Example

Let's run the program with different inputs:

  1. Entering the character 'A':
Enter a character: A
A is a vowel.
  1. Entering the character 'B':
Enter a character: B
B is a consonant.
  1. Entering a non-alphabet character:
Enter a character: 5
Invalid input. Please enter a valid alphabet character.

Explanation

  • The program prompts the user to enter a character.
  • It checks whether the entered character is a letter using Char.IsLetter.
  • If it is a letter, it further checks whether it is a vowel or a consonant.
  • The program handles invalid input by informing the user to enter a valid alphabet character.

Use

This program is useful when:

  • You need to determine whether a given character is a vowel or consonant in a C# application.
  • Basic decision-making and input validation are required.

Important Points

  • The program considers both uppercase and lowercase vowels.
  • It performs input validation to ensure that the user enters a valid alphabet character.
  • The program uses nested if-else statements for decision-making.

Summary

This C# program demonstrates a simple way to check whether a given character is a vowel or consonant. Understanding basic decision-making structures in C# is essential for building more complex applications with conditional logic.

Published on: