Photo by Pixabay
A simple C++ program to check whether a character is a vowel or a consonant. This program will take input character from user and by using selection statements it will tell the user that whether the given character is vowel or consonant.
#include <iostream>
using namespace std;
int main() {
char ch;
// Ask the user to enter a character
cout << "Enter a character: ";
cin >> ch;
ch = tolower(ch);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
{
cout << ch << " is a vowel." << endl;
}
else if ((ch >= 'a' && ch <= 'z'))
{
cout << ch << " is a consonant." << endl;
}
else {
cout << ch << " is not a valid alphabet character." << endl;
}
return 0;
}
In this example:
- We include the iostream header to use cin and cout for input and output.
- We declare a character variable ch.
- We ask the user to enter a character and store it in ch.
- We convert the character to lowercase using tolower to handle both uppercase and lowercase inputs.
- We check if the character is a vowel by comparing it to ‘a’, ‘e’, ‘i’, ‘o’, and ‘u’.
- If it’s a vowel, we print that it’s a vowel.
- If it’s an alphabet character but not a vowel, we print that it’s a consonant.
- If it’s not an alphabet character, we print that it’s not a valid alphabet character.
Leave a Reply