C++ Program to Check for Vowel or Consonant

In this example, you will ask a user to enter an Alphabet and check whether the Alphabet entered by the user is a vowel or consonant.

In the alphabet, a, e, i, o, and u are vowels and the rest are consonants.

Check vowel or consonant using if-else


#include<iostream>
using namespace std;

int main() {
  char c;
  cout << "Enter an Alphabet: ";
  cin >> c;
  if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' ||
    c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U') {
    cout << "\n " << c << " is a Vowel";
  } else {
    cout << "\n" << c << " is a consonant";
  }
  return 0;
}
output
Enter an Alphabet: A
A is a Vowel
Enter an Alphabet: d
d is a consonant

This program is a solution for when user input doesn't belong to a vowel or consonant.


#include<iostream>
using namespace std;

int main() {
  char c;
  cout << "Enter an Alphabet: ";
  cin >> c;
  if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z') {
    if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' || c == 'E' ||
      c == 'I' || c == 'O' || c == 'U')
      cout << "\n " << c << " is a Vowel";
    else
      cout << "\n" << c << " is a consonant";
  } else {
    cout << "\n" << c << " is a not belong to vowels or consonant";
  }
  return 0;
}
output
Enter an Alphabet: @
@ is a not belong to vowels or consonant

Time Complexity: O(1)