Write a C program to check whether an alphabet is vowel or consonant using if else. How to check vowels and consonants using if else in C programming. Program to input a character from user and check whether it is vowel or consonant in C. Logic to check vowel or consonant in C program.
Example
Input
Input character: a
Output
'a' is vowel
Required knowledge
Logic to check vowels or consonants
English alphabets 'a', 'e', 'i', 'o', 'u' both lowercase and uppercase are known as vowels and alphabets other than vowels are known as consonants.
Below is the step by step descriptive logic to check vowels or consonant.
- Read a character from user, store it in some variable say ch.
- Check conditions for vowel i.e. if ch == 'a', ch == 'e', ch == 'i', ch == 'o', ch == 'u'. Then it is vowel.
- If the given character is not vowel then check conditions for consonant. If character is alphabet and it is not vowel then it is consonant. Means check ch >= 'a' and ch <= 'z', then it is consonant
Let us implement this logic with both lower and upper case alphabets.
Program to check vowel or consonant
/** * C program to check whether a character is vowel or consonant */ #include <stdio.h> int main() { char ch; /* Reads a character from user */ printf("Enter any character: "); scanf("%c", &ch); // Condition for vowel if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' || ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U') { printf("%c is VOWEL.", ch); } else if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) { // Condition for consonant printf("%c is CONSONANT.", ch); } return 0; }
Note: You can also use ASCII character codes for checking vowels or consonants. Below program explains how to use ASCII character codes to check vowel and consonants.
Program to check vowel or consonant using ASCII values
/** * C program to check vowel or consonant using ASCII values */ #include <stdio.h> int main() { char ch; /* Reads any character from user */ printf("Enter any character: "); scanf("%c", &ch); // Condition for vowel 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 if((ch >= 97 && ch <= 122) || (ch >= 65 && ch <= 90)) { // Condition for consonant printf("%c is CONSONANT.", ch); } return 0; }
Also enhance your coding skills by learning this program using other approaches -
Enter any character: a a is VOWEL.
Happy coding ;)
You may also like
- If else programming exercise index.
- C program to check alphabets.
- C program to check alphabet, digit or special character.
- C program to print all alphabets form a - z.
- C program to check even or odd.
- C program to check whether a triangle is valid or not if its angles are given.
- C program to check whether a triangle is valid or not if its sides are given.
- C program to check whether a triangle is Equilateral, Isosceles or Scalene triangle.