Algorithm


Input: Accept a character as input.

Check if the character is an alphabet:

If the character is between 'a' and 'z' or between 'A' and 'Z', proceed to the next step.
If not, inform the user that the input is not an alphabet character.
Check if the character is a vowel or a consonant:

If the character is 'a', 'e', 'i', 'o', 'u' (case-insensitive), then it is a vowel.
If the character is not a vowel, it is a consonant.
Output: Display the result to the user.

Code Examples

#1 Code Example- C++ Programing to Determine Whether an Alphabet Character is a Consonant or a Vowel Using If-Else

Code - C++ Programming

//C++ Program to Check Whether a Character is Vowel or Consonant Using If-Else
#include<iostream>
using namespace std;
int main()
{
char input;
cout<<"Enter any alphabet: ";
//taking input alphabet from user
cin>>input;

//if condition to check if the input is vowel  
if
//conditional expression for vowel
(input=='a'||input=='e'||input=='i'||input=='o'||input=='u'||input=='A'||input=='E'||input=='I'||input=='O'||input=='U')
cout<<input <<" is a vowel";
    
//execute else if the input is not the vowel
else
cout<<input <<" is a consonant";
return 0;
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Enter any alphabet : d
d is a consonant

#2 Code Example- C++ Programing to Determine Whether an Alphabet Character is a Consonant or a Vowel Using If-Else Ladder.

Code - C++ Programming

/*C++ Program to Check Whether a Character is a Vowel or Consonant 
Using If-Else Ladder */

#include<iostream>
using namespace std;

int main()
{
//taking input alphabet from user
char input;
cout<<"Enter any alphabet: ";
cin>>input;

//Else-If ladder to check if input is vowel  
if(input=='a' || input=='A')
cout<<input<<" is a vowel";
else if(input=='e' || input=='E')
cout<<input<<" is a vowel";
else if(input=='i' || input =='I')
cout<<input<<" is a vowel";
else if(input=='o'|| input=='O')
cout<<input<<" is a vowel";
else if(input=='u' || input=='U')
cout<<input<<" is a vowel";

//execute else if the input is not a vowel
else
cout<<input<<" is a consonant";

return 0;
}

Copy The Code & Try With Live Editor

Output

x
+
cmd
Enter any alphabet: A
A is a vowel

#3 Code Example- C++ Programing to Determine Whether an Alphabet Character is a Consonant or a Vowel Using Switch-Case

Code - C++ Programming

#include<iostream>
using namespace std;

int main()
{

//taking input alphabet from user
char input;
cout<<"Enter any Alphabet: ";
cin>>input;

//Switch Case to check if the input is a vowel  
switch(input)
{
case 'a':
cout<<input<<" is a vowel";
break;
case 'e':
cout<<input<<" is a vowel";
break;
case 'i':
cout<<input<<" is a vowel";
break;
case 'o':
cout<<input<<" is a vowel";
break;
case 'u':
cout<<input<<" is a vowel";
break;
case 'A':
cout<<input<<" is a vowel";
break;
case 'E':
cout<<input<<" is a vowel";
break;
case 'I':
cout<<input<<" is a vowel";
break;
case 'O':
cout<<input<<" is a vowel";
break;
case 'U':
cout<<input<<" is a vowel";
break;

//execute default if the input is not a vowel
default:
cout<<input<<" is a consonant";
}
return 0;
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Enter any Alphabet: A
A is a vowel

#4 Code Example- Programing to Check When a Character is Neither Vowel nor Consonant (when a Non-alphabet Character is Entered. Like ‘7’ or ‘[‘ etc)

Code - C++ Programming

#include<iostream>
using namespace std;
int main()
{
 char input;
    cout<<"Enter any character :  ";
    cin>>input;
    //condition to check if input is an alphabet
    if((input>='a' && input<='z') || (input>='A' && input<='Z'))
    {
       //conditional expression for vowel
       int lowerVowel = (input=='a'||input=='e'||input=='i'||input=='o'||input=='u'
                         ||input=='A'||input=='E'||input=='I'||input=='O'||input=='U');
       
       //if condition to check if the input is vowel
        if(lowerVowel)
           cout<<input <<" is a vowel";
        
       //execute else if the input is not a vowel
        else
            cout<<input <<" is a consonant";
    }
    
   //execute else if the input is not the alphabet
    else
        cout<<input<<" is neither Vowel nor Consonant";
    cout<<endl;
    return 0;
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Enter any character: 4
4 is neither Vowel nor Consonant
Advertisements

Demonstration


C++ Programing Example  to Check Whether a character is Vowel or Consonant-DevsEnv