WAP to check the alphabet is vowel or not.
Objective
Write a C++ program to check if an alphabet character is a vowel or a consonant.
Algorithm / Approach
- Read a character
a. - Check for lowercase vowels using the Logical OR operator:
if (a=='a' || a=='e' || ...). - Check for uppercase vowels using
else if (a=='A' || a=='E' || ...). - If none match, use an
elseto declare it a consonant.
main.cpp
#include<iostream>
using namespace std;
int main() {
char a;
cout<<"Enter a Character: ";
cin>>a;
if(a=='a' ||a=='e' ||a=='i' ||a=='o' ||a=='u') {
cout<<"Character is vowel \n";
}
else if(a=='A' ||a=='E' ||a=='I'||a=='O' ||a=='U') {
cout<<"Character is vowel \n";
}
else {
cout<<"Character is NOT vowel \n";
}
return 0;
}
Expected Output
Enter a Character: E Character is vowel
Explanation of the Program
- The Logical OR operator (
||) is the counterpart to the Logical AND operator. - For an
||expression to be considered true, ONLY ONE of the conditions needs to be true. This is perfect for checking if a variable matches any value within a specific list.
Complexity
Time Complexity
O(1)
Space Complexity
O(1)