Extra 5% OFF Use Code: OL05
Free Shipping over ₹999

Character is a vowel or consonant

In this example we will check the character is a vowel or consonant

#include <iostream>
using namespace std;
int main(){
    char ch;
    int lowerCase,upperCase;
    cout<<"Enter character"<<endl;
    cin>>ch;

    // evaluates to 1 if variable c is a lowercase vowel
    lowerCase = (ch == 'a' || ch == 'i' || ch == 'e' || ch == 'o' || ch == 'u');

    // evaluates to 1 if variable c is a UpperCase vowel
    upperCase = (ch == 'A' || ch == 'I' || ch == 'E' || ch == 'O' || ch == 'U');

    if(lowerCase || upperCase)
        cout<<ch<< " is Vowel"<<endl;
    else
        cout<<ch<<" is Consonant"<<endl;

    return 0;
}

User Input: a

The code checks:

  • lowerCase = (ch == 'a' || ch == 'i' || ch == 'e' || ch == 'o' || ch == 'u') → This becomes true because ch == 'a'.
  • upperCase = (ch == 'A' || ch == 'I' || ch == 'E' || ch == 'O' || ch == 'U') → This becomes false because ch == 'A' is not true.

Since one of the conditions is true (lowerCase is true), the program prints:

Output:

a is Vowel

    Leave a Reply

    Your email address will not be published.

    Need Help?