In this tutorial we will learn how to write a C++ program that check the character is vowel or consonant.
Create this program we will use if else statement.
As we know that a,e,i,o and u are vowels and others are consonant in alphabets table.
In this program we will use two data types as char and int data type.
After it we will enter a character.
The character entered by the user is stored in variable c.
The isLowerCaseVowel evaluates to true if c is a lowercase vowel and false for any other character.
Similarly, isUpperCaseVowel evaluates to true if c is an uppercase vowel and false for any other character.
If both isLowercaseVowel and isUppercaseVowel is true, the character entered is a vowel , if not the character is a consonant.
In first output the input value of alphabet is I
And the result is
I is a vowel.
In second output the input value of alphabet is h
And the result is
h is consonant.
Create this program we will use if else statement.
As we know that a,e,i,o and u are vowels and others are consonant in alphabets table.
Explanation:
In this program we will use two data types as char and int data type.
After it we will enter a character.
The character entered by the user is stored in variable c.
The isLowerCaseVowel evaluates to true if c is a lowercase vowel and false for any other character.
Similarly, isUpperCaseVowel evaluates to true if c is an uppercase vowel and false for any other character.
If both isLowercaseVowel and isUppercaseVowel is true, the character entered is a vowel , if not the character is a consonant.
C++ program code
#include <iostream>
using namespace std;
int main()
{
char c;
int isLowercaseVowel, isUppercaseVowel;
cout << "Enter an alphabet: ";
cin >> c;
// evaluates to 1 (true) if c is a lowercase vowel
isLowercaseVowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
// evaluates to 1 (true) if c is an uppercase vowel
isUppercaseVowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');
// evaluates to 1 (true) if either isLowercaseVowel or isUppercaseVowel is true
if (isLowercaseVowel || isUppercaseVowel)
cout << c << " is a vowel.";
else
cout << c << " is a consonant.";
return 0;
}
In first output the input value of alphabet is I
And the result is
I is a vowel.
In second output the input value of alphabet is h
And the result is
h is consonant.
Please share this post and blog link with your friends.For more programs use this blog.
If you have any problem, please comment in comment box, subscribe this blog for notifications of new post on your email and follow this blog.
Created by-- HARSH CHAUHAN
No comments:
Post a Comment