Press ESC to close

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

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:

  1. We include the iostream header to use cin and cout for input and output.
  2. We declare a character variable ch.
  3. We ask the user to enter a character and store it in ch.
  4. We convert the character to lowercase using tolower to handle both uppercase and lowercase inputs.
  5. We check if the character is a vowel by comparing it to ‘a’, ‘e’, ‘i’, ‘o’, and ‘u’.
  6. If it’s a vowel, we print that it’s a vowel.
  7. If it’s an alphabet character but not a vowel, we print that it’s a consonant.
  8. If it’s not an alphabet character, we print that it’s not a valid alphabet character.

Leave a Reply

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