Vowel or Consonant Checker in C#
This C# program takes an alphabet character as input and checks whether it is a vowel or a consonant. The program evaluates the character against the standard vowels (a, e, i, o, u) and displays the result. This exercise introduces basic input handling, character comparison, and conditional logic in C#.
On this page
Source Code 💻
using System;
class Program
{
static void Main()
{
// Prompt user to enter a character
Console.Write("Enter an alphabet: ");
char ch = Char.ToLower(Console.ReadKey().KeyChar);
Console.WriteLine(); // For new line after input
// Check if the input is a letter
if (!Char.IsLetter(ch))
{
Console.WriteLine("Please enter a valid alphabet.");
}
else
{
// Check if the alphabet is a vowel or consonant
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
{
Console.WriteLine($"{ch} is a vowel.");
}
else
{
Console.WriteLine($"{ch} is a consonant.");
}
}
}
}
Explanation
- The program prompts the user to input a character.
- It converts the character to lowercase using Char.ToLower() to handle both uppercase and lowercase inputs.
- The program first checks if the input is a valid alphabet using Char.IsLetter(). If not, it asks for a valid input.
- If the input is a valid alphabet, it checks whether the character is a vowel by comparing it to ‘a’, ‘e’, ‘i’, ‘o’, and ‘u’.
- If the character is not a vowel, it is a consonant.
- The result is printed accordingly.
Example
-
Input: A
-
Output: a is a vowel.
-
Input: z
-
Output: z is a consonant.
🔚 end of document 🔚