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

In this tutorial, we will learn to check whether a character is vowel or consonant using C++.

So, here’s some quick information related to the program which can help you to know better. i.e, What actually says?

Quick Info:💡

↪ It’s a program to check whether an alphabet is vowel or consonant.

↪ Five alphabets a, e, i, o and u are known as vowels. All other alphabets except these 5 letters are known as consonants.

↪ If the alphabet is a vowel, it will print “Vowel” as the output on the output console but, if it is a consonant, it will print “Consonant” as the output on the output console.

↪ It can be done using different ways:

↪ Steps to check whether an alphabet is vowel or consonant:

  • First, Get any character by the user.
  • Apply the conditions according to the syntax of if-else/switch case/if-else ladder as your choice.
  • Print the result on the output console.

Check Whether a Character is Vowel or Consonant Using If-Else

If-else is a conditional statement that executes the code depending on the true or false.

In the If-else statement, a block of code is executed if the condition is evaluated true and another block of code is executed if the condition is false.

Syntax:

if(conditional expression)
<statement to be executed if condition is true>
else
<statement to be executed if condition is false>

Example:

#include<iostream.h>
#include<conio.h>
void main()
{
char alpha;
cout<<"Enter an alphabet:=";
cin>>alpha;
if(alpha=='a'||alpha=='e'||alpha=='i'||alpha=='o'||alpha=='u')
cout<<"It is vowel";
else
cout<<"It is consonant";
getch();
}
//Output:
Enter an alphabet:=r
It is consonant

Working:

Flow of a program:

check_if_else_work

Here, according to user input alpha = ‘r’ so, the condition becomes ‘false’ and right side get executed.

Check Whether a Character is Vowel or Consonant Using If-Else Ladder

An entire sequence of the if-else statements occur one after another is called as an if-else ladder.

Syntax:

if(conditional expression)
<statement>
else if(conditional expression)
<statement>
else if(conditional expression)
<statement>
....

Example:

#include<iostream.h>
#include<conio.h>
void main()
{
char alpha;
cout<<"Enter an alphabet:=";
cin>>alpha;
if(alpha=='a')
cout<<"It is vowel";
else if(alpha=='e')
cout<<"It is vowel";
else if(alpha=='i')
cout<<"It is vowel";
else if(alpha=='o')
cout<<"It is vowel";
else if(alpha=='u')
cout<<"It is vowel";
else
cout<<"It is consonant";
getch();
}
//Output:
Enter an alphabet:=o
It is vowel
check_if_else_ladder

Woking:

Flow of a program:

check_if_else_ladder_work

Check Whether a Character is Vowel or Consonant Using Switch-Case

A switch statement is a conditional statement that tests a value against different values.

If the value is matched, the corresponding group of statement is executed. If not then default statement is executed.

A switch statement begins with the ‘switch’ keyword followed by a value expression in the () parenthesis. Each case is separated by the break statement.

Syntax:

switch(expression)
{
case value1: statement;
break;
case value2: statement;
break;
.......
default: default statement;
}

Example:

#include<iostream.h>
#include<conio.h>
void main()
{
char alpha;
cout<<"Enter an alphabet:=";
cin>>alpha;
switch(alpha)
{
case 'a':
cout<<"It is vowel";
break;
case 'e':
cout<<"It is vowel";
break;
case 'i':
cout<<"It is vowel";
break;
case 'o':
cout<<"It is vowel";
break;
case 'u':
cout<<"It is vowel";
break;
default:
cout<<"It is consonant";
}
getch();
}
//Output:
Enter an alphabet:=i
It is vowel
check_switch_case

Working:

Flow of a program:

check_switch_case_work

Well, I hope it really helped you to know the C++ better.

Attention reader⚠ Don’t stop learning now.

You must try this program using class in free time, one of the class examples is addition using class.

Just stay with us to get more such exciting codes to explore more in C++.

Leave a Reply

Your email address will not be published. Required fields are marked *