In this article, you will learn to check whether an alphabet entered by the user is a vowel or a consonant.
English alphabets a, e, i, o and u both lowercase and uppercase are known as vowels. Alphabets other than vowels are known as consonants.
We check whether given character matches any of the 5 vowels. If yes, we print “Vowel”, else we print “Consonant”.
Examples :
Input : x = 'd' Output : Consonant Input : x = 'u' Output : Vowel
Implementation :
1. Check if else condition :
// C Program to Check Vowel or Consonant #include <stdio.h> int main() { // take input alphabet char ch ='d'; // check if input is Vowel : a, e, i, o, u if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') { // then print vowel printf("'%c' is a VOWEL\n", ch); } else // else print consonant printf("'%c' is a CONSONANT\n", ch); // successful completion return 0; }
Output :
'd' is a CONSONANT
2. Check using ASCII value :
Internally characters in C is represented by an integer value known as ASCII value. You can also use ASCII value of a character to check vowels and consonants.
ASCII value of A=65, E=69 … a=97, e=102 etc.
// C Program to Check Vowel or Consonant #include <stdio.h> int main() { // take input alphabet char ch ='d'; // check if input is Vowel : a, e, i, o, u if(ch==97 || ch==101 || ch==105 || ch==111 || ch==117 || ch==65 || ch==69 || ch==73 || ch==79 || ch==85) { printf("'%c' is Vowel", ch); } // else print consonant else if((ch >= 97 && ch <= 122) || (ch >= 65 && ch <= 90)) { printf("'%c' is Consonant", ch); } else { //either vowel nor consonant printf("%c is not an alphabet", ch); } // successful completion return 0; }
Output :
'd' is a CONSONANT
3. Check vowel using switch statement :
// C Program to Check Vowel or Consonant #include <stdio.h> int main() { // take input alphabet char ch ='d'; // switch cases switch(ch) { case 'a': case 'A': case 'e': case 'E': case 'i': case 'I': case 'o': case 'O': case 'u': case 'U': //print consonant printf("'%c' is Consonant", ch); break; default: // check if input is Vowel : a, e, i, o, u printf("'%c' is Vowel", ch); } // successful completion return 0; }
Output :
'd' is a CONSONANT
Please write comments if you find anything incorrect. A gentle request to share this topic on your social media profile.