codescracker
c++

C++ Program to Check Vowel or Consonant



« Previous Program Next Program »


In this article, you will learn and get code on checking whether the given alphabet is a vowel or consonant in C++. The program is created in following approaches:

Important - If the pronunciation of a character produced by humans, when the breath flows out through mouth without being blocked by teeth, lips or tongue. Then the character is called as vowel.

List of Vowels

There are 5 vowels, that are a, e, i, o, u or A, E, I, O, U. On the basis of these 5 vowels, we have created the program, that checks whether input character by user at run-time is a vowel or not.

Note - Sometime, y can also be called as vowel.

As already mentioned, we'll only deal with 5 vowels as listed above. We're not going in deep about vowel, because we only have to create the program on it. So let's move on to the program.

Check Vowel or Consonant using if-else

To check whether the input alphabet is a vowel or not (consonant) in C++ programming, you have to ask from user to enter an alphabet first, to check and print the message as shown in the program given below:

If the input alphabet is equal to any of the following 10 characters (5 lowercase vowel and 5 uppercase vowel):

The it is called as vowel. Otherwise the alphabet is a consonant. Here is the program:

// C++ Program to Check Vowel or Consonant using if-else
// ----codescracker.com----

#include<iostream>
using namespace std;
int main()
{
    char ch;
    cout<<"Enter an Alphabet: ";
    cin>>ch;
    if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u')
        cout<<"\nIt is a Vowel";
    else if(ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U')
        cout<<"\nIt is a Vowel";
    else
        cout<<"\nIt is a Consonant";
    cout<<endl;
    return 0;
}

This program was build and run under Code::Blocks IDE. Here is its sample run:

C++ program check vowel

Now supply any alphabet say U and press ENTER key to see the following output:

program check vowel c++ programming

Here is another sample run with user input as c:

check vowel or consonant c++

This program has a limitation, that if user enters 4, then the output produced by above program is, It is a Consonant. But 4 is not a consonant, rather it is a number. Therefore we have another program to deal with this type of input.

What if User enters neither Vowel nor Consonant ?

The program given below is the solution of user-input that is neither vowel nor consonant:

// Check Vowel or Consonant in C++
// -----codescracker.com------

#include<iostream>
using namespace std;
int main()
{
    char ch;
    cout<<"Enter the Character: ";
    cin>>ch;
    if((ch>='a' && ch<='z') || (ch>='A' && ch<='Z'))
    {
        int lowerVowel, upperVowel;
        lowerVowel = (ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u');
        upperVowel = (ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U');
        if(lowerVowel)
            cout<<"\nIt is a lowercase Vowel";
        else if(upperVowel)
            cout<<"\nIt is an uppercase Vowel";
        else
            cout<<"\nIt is a Consonant";
    }
    else
        cout<<"\nIt is neither Vowel nor Consonant";
    cout<<endl;
    return 0;
}

Here is its sample run with user input as 4:

check vowel consonant if else c++

And here is another sample run with user input as E:

check vowel c++

The statement,

lowerVowel = (ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u');

initializes 1 to lowerVowel, if the value of ch gets matched to any of the character a, e, i, o, u. Because 1 as condition of if block treats as a true condition, therefore program flow goes inside the if's body, if lowerVowel holds its value as 1. Otherwise, in similar way, if-else block gets executed. And if both lowerVowel and upperVowel does not holds its value as 1, then else's body gets executed, that prints a message, It is a Consonant

Check Vowel using switch

This is the last program that uses switch case to check and print whether input character is a vowel or consonant:

// Check Vowel or Consonant using switch
// ----codescracker.com----

#include<iostream>
using namespace std;
int main()
{
    char ch;
    cout<<"Enter an Alphabet: ";
    cin>>ch;
    switch(ch)
    {
        case 'a':
        case 'A':
        case 'e':
        case 'E':
        case 'i':
        case 'I':
        case 'o':
        case 'O':
        case 'u':
        case 'U':
            cout<<"\nIt is a Vowel";
            break;
        default:
            cout<<"\nIt is a Consonant";
            break;
    }
    cout<<endl;
    return 0;
}

It will produce the same output as of very first program in this article.

Same Program in Other Languages

You may like the same program in other programming languages:


« Previous Program Next Program »