Loading...
C++ Program to Check Whether a Character is Vowel or Consonant.

C++ Program to Check Whether a Character is Vowel or Consonant.

In this example, we used if...else statement and ternary operator to Check Whether a Character is Vowel or Consonant.

To understand this example, you should have the knowledge of the following C++ programming topics:

Program to Check Whether a Character is Vowel or Consonant.

  • Five alphabets a, e, i, o and u are known as vowels.
  • All other alphabets except these 5 alphabets are known are consonants.
  • Now in here we will see how we can identify whether a character is a vowel or consonant using the C++ pro
  • This program assumes that the user will always enter an alphabet.

Example: Program to Check Vowel or a Consonant Manually.

// Program to check a Vowel or a Consonant Manually

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

Output

Enter an alphabet: u
u is a vowel.

Working of above Program
  • This C++ program allows the user to enter any character and check whether the user specified character is Vowel or Consonant using If Else Statement.
  • This program takes the character value(entered by user) as input.
  • And checks whether that character is a vowel or consonant using if-else statement.
  • The character entered by the user is stored in variable c.
  • The isLowerCaseVowel evaluates to true if c is a lowercase vowel and false for any other character.
  • Similarly, isUpperCaseVowel evaluates to true if c is an uppercase vowel and false for any other character.
  • If both isLowercaseVowel and isUppercaseVowel is true, the character entered is a vowel , if not the character is a consonant.

Next Example

We hope that this Example helped you develop better understanding of the concept of Whether an Alphabet is Vowel or Not in C++.

Keep Learning : )

In the next Example, we will learn about C++ Find Largest Number Among Three Numbers.


- Related Topics