C# Program to Check Vowel or Consonant
In C#, you can check if a character is a vowel or consonant by using
char.ToLower() to normalize the input and then comparing it with vowels using if statements, for example: if ("aeiou".Contains(char.ToLower(ch))) Console.WriteLine("Vowel"); else Console.WriteLine("Consonant");.Examples
Inputa
OutputVowel
InputB
OutputConsonant
Inputz
OutputConsonant
How to Think About It
To check if a letter is a vowel or consonant, first convert the letter to lowercase to handle both uppercase and lowercase inputs. Then check if this lowercase letter is one of the vowels 'a', 'e', 'i', 'o', or 'u'. If yes, it is a vowel; otherwise, it is a consonant.
Algorithm
1
Get a single character input from the user.2
Convert the character to lowercase to simplify comparison.3
Check if the character is in the set of vowels (a, e, i, o, u).4
If it is a vowel, print 'Vowel'.5
Otherwise, print 'Consonant'.Code
csharp
using System; class Program { static void Main() { Console.Write("Enter a letter: "); char ch = Console.ReadKey().KeyChar; Console.WriteLine(); char lowerCh = char.ToLower(ch); if ("aeiou".Contains(lowerCh)) Console.WriteLine("Vowel"); else Console.WriteLine("Consonant"); } }
Output
Enter a letter: a
Vowel
Dry Run
Let's trace input 'B' through the code
1
Input character
User inputs 'B'
2
Convert to lowercase
'B' becomes 'b'
3
Check if vowel
'b' is checked against 'aeiou' and is not found
4
Print result
Print 'Consonant'
| Step | Character | Lowercase | Is Vowel? | Output |
|---|---|---|---|---|
| 1 | B | b | No | Consonant |
Why This Works
Step 1: Normalize input
Using char.ToLower() ensures the program treats uppercase and lowercase letters the same.
Step 2: Check vowels
The program checks if the lowercase character is in the string "aeiou", which contains all vowels.
Step 3: Output result
If the character is found in vowels, it prints Vowel, otherwise it prints Consonant.
Alternative Approaches
Using switch statement
csharp
using System; class Program { static void Main() { Console.Write("Enter a letter: "); char ch = char.ToLower(Console.ReadKey().KeyChar); Console.WriteLine(); switch (ch) { case 'a': case 'e': case 'i': case 'o': case 'u': Console.WriteLine("Vowel"); break; default: Console.WriteLine("Consonant"); break; } } }
Switch is clear and easy to read but slightly longer than string.Contains.
Using HashSet for vowels
csharp
using System; using System.Collections.Generic; class Program { static void Main() { var vowels = new HashSet<char>{'a','e','i','o','u'}; Console.Write("Enter a letter: "); char ch = char.ToLower(Console.ReadKey().KeyChar); Console.WriteLine(); if (vowels.Contains(ch)) Console.WriteLine("Vowel"); else Console.WriteLine("Consonant"); } }
HashSet lookup is very fast and good for larger sets but overkill for just vowels.
Complexity: O(1) time, O(1) space
Time Complexity
Checking if a character is in a small fixed set of vowels is constant time, O(1), because the set size does not grow.
Space Complexity
The program uses a fixed amount of space for the vowel set and variables, so space complexity is O(1).
Which Approach is Fastest?
Using string.Contains or a switch statement both run in constant time; HashSet is also O(1) but uses more memory.
| Approach | Time | Space | Best For |
|---|---|---|---|
| string.Contains | O(1) | O(1) | Simple and readable for small sets |
| switch statement | O(1) | O(1) | Clear control flow, easy to extend |
| HashSet lookup | O(1) | O(n) | Fast lookup for larger sets, more memory |
Always convert the input character to lowercase before checking to handle both uppercase and lowercase letters easily.
Forgetting to convert the input to lowercase causes uppercase vowels to be incorrectly identified as consonants.