To check whether the input alphabet is a vowel or not a vowel in C++ Programming, you have to ask to the user to enter a character and check the character for vowel. The character is vowel, only if it is equal to a, A, e, E, i, I, o, O, u, U.
Problem Description
The program takes a character and checks if it is a vowel, consonant or a digit.
This is a C++ Program to Check if a Character is a Vowel, Consonant or Digit.
Problem Solution
1. A character is entered.
2. Using nested if else condition, the character is checked if it is a vowel, consonant or a digit.
3. The result is printed.
4. Exit.
Source Code:

#include <iostream>
using namespace std;
int main()
{
    char c;
    int isLowercaseVowel, isUppercaseVowel;
    cout << "Enter an alphabet: ";
    cin >> c;
   
    isLowercaseVowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
    
    isUppercaseVowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');
    
    if (isLowercaseVowel || isUppercaseVowel)
        cout << c << " is a vowel.";
    else
        cout << c << " is a consonant.";
    return 0;
}



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 −

Another Example

#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 −

Another Example

#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;

Post a Comment

 
Top