C++ program to check vowel or consonant using if else, It will either vowel or consonant
In this article, you will learn how to make a C++ program to check whether a character is a vowel or consonant using if-else in the C++ programming language.
Examples
Enter a character to check: K
'K' is a consonant character.
Enter a character to check: 24
Sorry, You entered Non-Alphabetic character!
You should have knowledge of the following topics in c++ programming to understand this program:
- C++ Cctype library
- C++
if-else
condition statement - C++
cin
object - C++
cout
object
Vowel Characters
Consonant Characters
Source Code
// C++ program to check vowel or consonant using if else, It will either vowel or consonant
#include <bits/stdc++.h>
#include <cctype>
// this header file loaded for check the charcters
using namespace std;
int main() {
char random_character;
int lowercase = 0, uppercase = 0;
cout << "Enter a character to check: ";
cin >> random_character;
// isalpha() function will check
// whether character is alphabetic or non-alphabetic
if (random_character && isalpha(random_character)) {
// it will be return 1
// if input character matched with these small characters
lowercase = random_character == 'a' || random_character == 'e' || random_character == 'i' || random_character == 'o' || random_character == 'u';
// it will be return 1
// if input character matched with these capital characters
uppercase = random_character == 'A' || random_character == 'E' || random_character == 'I' || random_character == 'O' || random_character == 'U';
if (lowercase || uppercase)
cout << "\n'" << random_character << "' is a vowel character.\n";
else
cout << "\n'" << random_character << "' is a consonant character.\n";
} else {
cout << "\nSorry, You entered Non-Alphabetic character!\n";
}
return 0;
}
Output
Enter a character to check: K
'K' is a consonant character.
Explanation
In this program, we have taken input K
from the user and checked for its alphabetic or non-alphabetic character.
Then we matched this character with a series of vowel characters, If matched with the vowel characters series then it's vowel character.
If didn't match with the vowel characters series then It's a consonant character.