Check If a Character Is a Consonant or A Vowel in C++


Write a C++ Program to check if a character or a letter is a consonant or a vowel.

Click Here to View the Solution!
#include <iostream>
using namespace std;
int main()
    char c;
    int capital, small;
    cout << "Enter an alphabet:\n ";
    cin >> c;
    // true (1) if c is a small vowel
    small =(c == 'a'||c == 'e'||c == 'i'||c == 'o'||c == 'u');
    // true (1) if c is a capital vowel
    capital =(c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');
    // true (1) if either LCVowel or UCVowel is true
    if (small || capital)
        cout << c << " is a vowel";
        cout << c << " is a consonant";
    return 0;
Click Here to View the Output!
Enter an alphabet:
B is a consonant
Click Here to View the Explanation!
  • This program uses if... else, and nested if... else to check if the alphabets are vowels. a, e, i, o, and u are the only consonants. A code is compiled to check if the alphabet is one of these.
  • To insert a character, we have to declare a variable as a character. Hence, we add char c. Then we declare two more integers: capital and small. This is because the entered vowel can be in capital or small form.
  • One way to use a nested if else is through OR operators.
  • || is an operator used for OR. If the entered character is uppercase, capital is marked true or 1. The first output is displayed. If marked false, the second output is displayed.