In this article, I will be writing a C# Program to check vowels or consonants. I will be explaining the whole program stepwise. It is a common interview question for freshers.
Program Overview
We are going to solve a commonly asked question in C# Programming. We are going to write a C# Program to check Vowels or Consonants in the entered alphabets. I will be using the switch-case method in C# to check vowels and consonants.
C# Program to Check Vowels or Consonants
Here’s the C# Program to check Vowels or Consonants:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class exercise16
{
static void Main(string[] args)
{
char ch;
Console.Write("\n\n");
Console.Write("Vowel or Consonant checker tool\n");
Console.Write("-----------------------------------------------------");
Console.Write("\n\n");
Console.Write("Please Enter an Alphabet (A-Z or a-z) : ");
ch = Convert.ToChar(Console.ReadLine().ToLower());
int i=ch;
if(i>=48 && i<=57)
{
Console.Write("Err! Please enter an alphabet not a number.");
}
else
{
switch (ch)
{
case 'a':
Console.WriteLine("Entered alphabet is vowel");
break;
case 'i':
Console.WriteLine("Entered alphabet is vowel");
break;
case 'o':
Console.WriteLine("Entered alphabet is vowel");
break;
case 'u':
Console.WriteLine("Entered alphabet is vowel");
break;
case 'e':
Console.WriteLine("Entered alphabet is vowel");
break;
default:
Console.WriteLine("Entered alphabet is a Consonant");
break;
}
}
Console.ReadKey();
}
}
Program Output
Here’s the output of the above program:
Vowel or Consonant checker tool
-----------------------------------------------------
Please Enter an Alphabet (A-Z or a-z) : C
Entered alphabet is a Consonant