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

C++ProgrammingServer Side Programming

Vowels are the alphabets a, e, i, o, u. All the rest of the alphabets are known as consonants.

The program to check if a character is a vowel or consonant is as follows −

Example

 Live Demo

#include <iostream>
using namespace std;
int main() {
   char c = 'a';
   if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' )
   cout <<c<< " is a Vowel" << endl;
   else
   cout <<c<< " is a Consonant" << endl;
   return 0;
}

Output

a is a Vowel

In the above program, an if statement is used to find if the character is a, e, i, o or u. If it is any of these, it is a vowel. Otherwise, it is a consonant.

This is shown in the below code snippet.

if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' )
cout <<c<< " is a Vowel" << endl;
else
cout <<c<< " is a Consonant" << endl;

The above program checks only for lower case characters. So, a program that checks for upper case as well as lower case characters is as follows −

Example

 Live Demo

#include <iostream>
using namespace std;
int main() {
   char c = 'B';
   if (c == 'a' || c == 'e' || c == 'i' ||
   c == 'o' || c == 'u' || c == 'A' ||
   c == 'E' || c == 'I' || c == 'O' || c == 'U')
   cout <<c<< " is a Vowel" << endl;
   else
   cout <<c<< " is a Consonant" << endl;
   return 0;
}
B is a Consonant

In the above program, an if statement is used to find if the character is a, e, i, o or u (both in upper case as well as lower case).. If it is any of these, it is a vowel. Otherwise, it is a consonant.

if (c == 'a' || c == 'e' || c == 'i' ||
c == 'o' || c == 'u' || c == 'A' ||
c == 'E' || c == 'I' || c == 'O' || c == 'U')
cout <<c<< " is a Vowel" << endl;
else
cout <<c<< " is a Consonant" << endl;
raja
Published on 26-Sep-2018 17:22:09
Advertisements