Google Add

Search

C++ Program to Check whether a Character is Vowel or Consonant

Write a C++ program to check whether a character is vowel or consonant. Given an input character, we have to write a code to check whether an entered character is vowel or consonant.

An alphabet a, e, i, o, u is called as vowel and the rest of the alphabet is called as a consonant.

How to check whether a character is vowel or consonant


i) Take an input alphabet from user.

ii) Check whether an input alphabet is a, e, i, o, u, A, E, I, O, U then it's a vowel otherwise it's a consonant.

C program to check whether a character is vowel or consonant


C++ Program to Check whether a Character is Vowel or Consonant



C++ Program to Check whether a Character is Vowel or Consonant


In this programming example, we are going to write a C++ code which takes an input character and check whether a character is vowel or consonant.


#include <iostream>
using namespace std;

int main() {

   // Declare char
   char alpha;

   // Take a input character from a user
   cout<<" Enter an alphabet \n";
   cin >> alpha;

   if ( alpha == 'a' || alpha == 'A'  
       || alpha == 'e' || alpha == 'E' 
         || alpha == 'i' || alpha == 'I' 
       || alpha =='o' || alpha=='O' 
        || alpha == 'u' || alpha == 'U') {

         cout << alpha << " is a vowel";
      } else {
          cout << alpha << " is a consonant";
       }

       return 0;
}


Output:

Enter an alphabet :  a

a is a vowel.

C++ Program to Check whether a Character is Vowel or Consonant by using Function


In this example, we have created separate function to check whether a character is vowel or consonant. When a user input an alphabet, we called isVowel() method, which return true if the input character is vowel otherwise it returns false.


#include <iostream>
using namespace std;


bool isVowel(char alpha) {
    
    if ( alpha == 'a' || alpha == 'A'  
       || alpha == 'e' || alpha == 'E' 
         || alpha == 'i' || alpha == 'I' 
       || alpha =='o' || alpha=='O' 
         || alpha == 'u' || alpha == 'U') {

        return true;
       
    } else {
       
        return false;
    }
    
}

int main() {

   char alpha;

   // Take a input character from a user
   cout << "Enter an alphabet \n";
   cin >> alpha;

   
    if(isVowel(alpha)) {
        cout << alpha << " is vowel ";
    } else {
        cout << alpha << " is consonant ";
    }

    return 0;
}



Programming questions for beginners

C++ program to print even numbers between 1 to 100 using for and while loop

Programming questions on arrays

No comments:

Post a Comment