C++ Program to Vowel or Consonant

June 13, 2023, Learn eTutorial

How can we check whether the character is a vowel or consonant?

Among the 26 letters in the alphabet, 5 are vowels ( a, e, I, o, u ), and the rest are consonants.

To check whether the given character is a vowel or consonant, we have to compare the entered character with the vowel letters ( a, e, i, o, u ). 

  • If it is true then the entered character is a vowel. 
  • If the character is an alphabet but not a vowel, then it is a consonant.
  • If it is neither vowel nor consonant, then it is not an alphabet.

How can we check for vowels and consonants in C++?

Ask the user to enter input and store it into a character type variable c. The entered character maybe

  • Uppercase letter
  • Lowercase letter
  • Not alphabet

So, we have to check for the above three options. For that, declare two variables 'lowercase' and ''uppercase' of boolean type. Boolean is a data type that is declared with the keyword bool and can take only the values true or false. True = 1 and false = 0.

  1. First, check whether it is an alphabet or not. For this, the Function isalpha can be used. Isalpha()is a function in C++ used to check if the given character is an alphabet or not.
  2. Next, we have to check for upper-case vowels. For this uppercase = ( c = ‘A’ // ‘E’ //‘I’ // ‘O’ // ‘U’ ); If this evaluates to 1, it is a vowel.
  3. Now, have to check for lowercase vowels. For this, lowercase = ( c = ‘a’ // ‘e’ // ‘i’ // ‘o’ // ‘u’ ); If this evaluates to 1, it is a vowel.
  4. Finally, If the entered character is an alphabet and not a lowercase or uppercase vowel then it is a consonant.


Step 1:  Call the header file iostream.

Step 2: Use the namespace std.

Step 3: Open the main function int main().

Step 4: Declare a character type variable c, bool type variables lowercase, uppercase.

Step 5: Print a message “Enter a character”.

Step 6: Read the user input to the variable c.

Step 7: check if the user input is an alphabet. If false, print a non-alphabetic character. 

Step 8: Check the user input is an uppercase vowel  If true; Print c is a vowel. 

Step 9: Check the character is a lower case vowel. If true; Print c is a vowel.

Step 10: If step 7 is true and step 8 and step 9 are false; then print c is consonant.

Step 11: Exit.

C++ Source Code

                                          #include <iostream>
using namespace std;

int main() {
    char c;
    bool lowercase, uppercase;

    cout << "Enter an alphabet: ";
    cin >> c;

    // evaluates to 1 (true) if c is a lowercase vowel
   lowercase = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');

    // evaluates to 1 (true) if c is an uppercase vowel
   uppercase= (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');

    // show error message if c is not an alphabet
    if (!isalpha(c))
      printf("Error! Non-alphabetic character.");
    else if (lowercase || uppercase)
        cout << c << " is a vowel.";
        cout << c << " is a consonant.";

    return 0;


Run 1
Enter an alphabet: E
E is a vowel.
Run 2
Enter an alphabet: a
a is a vowel.
Run 3
Enter an alphabet: h
h is a consonant.
Run 4
Enter an alphabet: T
T is a consonant.
Run 5
Enter an alphabet: 5
Error! Non-alphabetic character.