C++ program to check given alphabet is vowel or consonant - coderforcoders

Search

Wednesday, 13 March 2019

C++ program to check given alphabet is vowel or consonant

Vowel or consonant in C++

Theory:

Vowel or consonant in C++ is a simple C++ program to check a given alphabet is vowel or consonant. An user will enter an alphabet and the program will check whether the given alphabet is vowel or consonant and outputs the result. Vowel is a class declared and int checkVowel() is the member function which is used to check whether the given alphabet is vowel or consonant.

Logic:

This program is constructed using if and else if statements. Letter 'a', 'e', 'i', 'o', 'u' or 'A', 'E', 'I', 'O', 'U' from English alphabet are known as vowels and the letters which are not vowels are called as consonants. The same should checked with the given input. We can use OR operation ( || called as pipeline) to execute multiple conditional statements  in a single if statement.


if( c == 'a' || c == 'e' || c == 'i' || c == 'o' || c=='u' )
return 1;
else if( c == 'A' || c == 'E' || c == 'I' || c == 'O' || c=='U' )
return 1;
return 0;
It returns the value 1 if the given input is a vowel, else it returns the value 0.

Program:

C++ program to check given alphabet is vowel or consonant.


#include<iostream>
using namespace std;
class vowel{
public:
int checkVowel(char c)
{
if( c == 'a' || c == 'e' || c == 'i' || c == 'o' || c=='u' )
return 1;
else if( c == 'A' || c == 'E' || c == 'I' || c == 'O' || c=='U' )
return 1;
return 0;
}
};
int main()
{
int result;
char c;
vowel obj1;
cout<<"Please enter an alphabet\n";
cin>>c;
result=obj1.checkVowel(c);
if(result)
cout<<c<<" is a vowel ";
else
cout<<c<<" is a consonant ";
cout<<endl;
}

Output 1:
Please enter an alphabet
a
a is a vowel

Output 2:
Please enter an alphabet
A
A is a vowel

Output 3:
Please enter an alphabet
Z
Z is a consonant


No comments:

Post a Comment