In this program, we will learn how to check whether a character is a vowel or consonant using C++ Programming language.
In the English language, there are 26 alphabets. Out of which, 21 are consonants and 5 are vowels. The 5 vowels are A, E, I, O, and U. Remaining all alphabets are consonants.
We will check whether the entered character is a vowel or consonant using the If-Else statement.
So, without further ado, let’s begin this post.
C++ Program to Check Whether a Character is Vowel or Consonant
// C Program to Check Whether a Character is Vowel or Consonant #include <iostream> using namespace std; int main(){ char ch; // Asking for input cout << "Enter an alphabet: "; cin >> ch; // Checking whether the character is vowel or consonant if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U'){ cout << ch << " is a vowel." << endl; } else { cout << ch << " is a consonant." << endl; } return 0; }
Output 1
Enter an alphabet: A
A is a vowel.
Output 2
Enter an alphabet: z
z is a consonant.
How Does This Program Work?
char ch;
In this program, we have declared a character data type variable named ch.
// Asking for input cout << "Enter an alphabet: "; cin >> ch;
The user is asked to enter a character. The entered character is stored in the ch named variable.
// Checking whether the character is vowel or consonant if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U'){ cout << ch << " is a vowel." << endl; }
Now, we check whether the entered alphabet is equal to either of the lowercase or uppercase vowels. If the condition is True, then this program will display that the entered character is a vowel.
else { cout << ch << " is a consonant." << endl; }
Otherwise, this program will display that the entered character is a consonant.
Conclusion
I hope after going through this post, you understand how to check whether a character is a vowel or consonant using C++ Programming language.
If you have any doubt regarding the program, then contact us in the comment section. We will be delighted to assist you.
Also Read: