➤ Hello World! in C++
➤ Print Number in C++
➤ Add 2 Numbers C++
➤ Arithmetic Operation
➤ Sum Avg of 3 Number
➤ Area Program in C++
➤ Simple Interest in C++
➤ Find ASCII value in C++
➤ Swap 2 Number in C++
Flow Control Programs
➤ Even-Odd in C++
➤ +ve, -ve, 0 in C++
➤ Vowel-Consonant
➤ Greatest of 3 no.
➤ Check Leap Year
➤ Calculator Program
➤ Reverse a Number
➤ Sum of Natural Number
➤ GCD of 2 Number
➤ LCM of 2 Number
➤ Find Power in C++
➤ Fibonacci Series in C++
➤ Palindrome Number
➤ Find Factorial in C++
➤ Factorial Using Recursion
➤ Prime Number in C++
➤ Prime Number b/w 1-N
Array
➤ Linear Search in C++
➤ Binary Search in C++
Others
➤ Introduction to C++
➤ Data Types in C++
➤ Range of Data Types
➤ Void main, main vs int main
In this post, we will write a C++ program to check vowel or consonant. First, we will write a program using the if-else statement and then we will write a C++ program to check vowel or consonant using the switch case statement.
C++ Program to check whether the character is vowel or consonant using if-else
#include<iostream>
using namespace std;
int main()
{
// declare variables
char ch;
int uppercase, lowercase;
// take input
cout << "Enter a Character: ";
cin >> ch;
// evaluates to 1(true) if ch is a uppercase Vowel
uppercase = (ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U');
// evaluates to 1(true) if ch is a lowercase Vowel
lowercase = (ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u');
// evaluates to 1(true) if either uppercase or lowercase is true
if(uppercase || lowercase)
cout << ch << " is Vowel." << endl;
else
cout << ch << " is Consonant." << endl;
return 0;
}
Output for the different test-cases:-
Enter a Character: V
V is Consonant.
Enter a Character: a
a is Vowel.
In this program, for uppercase and lowercase vowel alphabets, we take two variables and then compared it. But it is not required to take these variables, we can do the same as the below program,
#include<iostream>
using namespace std;
int main()
{
// declare variable
char ch;
// take input
cout << "Enter a character:";
cin >> ch;
// check alphabet
if( (ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U') ||
(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u') )
cout << ch << " is vowel \n";
else cout << ch << " is consonant \n";
return 0;
}
C++ Program to Check Vowel or Consonant using Switch
In the above programs, we have written conditions separately for the uppercase and lowercase characters. And it increases the length of the code. We can solve this using tolower() or toUpper() function.
The idea is to, convert the input into either in uppercase or lowercase and write condition only for them (uppercase or lowercase). If we will convert the input into lowercase then we have to write the condition only for the lowercase character.
The tolower() function takes a character and returns a lowercase character. Similarly, toUpper() function converts the given character into an uppercase character.
#include<iostream>
using namespace std;
int main()
{
// declare variable
char ch;
// take input
cout << "Enter a character : ";
cin >> ch;
// convert uppercase to lowercase
ch = tolower(ch);
// check alphabet
switch(ch)
{
case 'a' :
cout << "Vowel";
break;
case 'e' :
cout << "Vowel";
break;
case 'i' :
cout << "Vowel";
break;
case 'o' :
cout << "Vowel";
break;
case 'u' :
cout << "Vowel";
break;
default :
cout << "Consonant";
}
return 0;
}
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!
Learn More C++ Programming Examples,
- Introduction to C++ Programming
- Data Types in C++ Programming
- Find Range of Data Types in C++
- Void main(), main() vs int main() in C++
- Hello World! Program in C++
- Addition of Two Numbers in C++
- Add Subtract Multiply Divide 2 Numbers
- Sum and Average of three numbers in C++
- Area of Circle Triangle Rectangle in C++
- Calculate Simple Interest in C++
- C++ program to find ASCII value
- C++ program to swap two Number
- Check Even or Odd Number in C++
- Find Positive Negative Zero in C++
- Check Vowel or Consonant in C++
- Greatest of Three Numbers in C++
- Program to Check Leap Year in C++
- Simple Calculator Program in C++
- Program to Reverse a Number in C++
- Sum of Natural Numbers in C++
- C++ Program for GCD of Two Numbers
- C++ Program for LCM of Two Numbers
- Find Power of a Number in C++
- Fibonacci Series Program in C++
- Check Palindrome Number in C++
- Factorial of a Number Program in C++
- Factorial Using Recursion in C++
- Prime Number Program in C++
- Prime Number b/w 1 to N in C++
Hello, it’s me, I am also visiting this website on a regular basis, this website is truly fastidious and the users are in fact sharing pleasant thoughts.
I think the admin of this website is actually working hard for hisweb page, because here every data is quality based information.