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. C Program to input a character from user and check whether it is vowel or consonant. Logic to check vowel or consonant in C programming.
Input
Input character: a
Output
'a' is vowel
Required knowledge
Basic C programming, Relational operators, Logical operators, If else
Logic to check vowels or consonants
English alphabets a, e, i, o and u both lowercase and uppercase are known as vowels. Alphabets other than vowels are known as consonants.
Step by step descriptive logic to check vowels or consonant.
- Input 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 character is alphabet but not vowel then it is consonant. Means check
ch >= 'a' && ch <= 'z'
then, it is consonant. - If it is neither vowel nor consonant, then it is not alphabet.
Character in C is represented inside single quote. Do not forget to add single quote whenever checking for character constant.
Let us implement the 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;
/* Input 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);
}
else
{
/*
* If it is neither vowel nor consonant
* then it is not an alphabet.
*/
printf("'%c' is not an alphabet.", ch);
}
return 0;
}
Logic to check vowel or consonant 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. So, instead of checking condition if(ch=='a' || ch=='e' || ... )
you can use if(ch==97 || ch==102 || ...)
Program to check vowel or consonant using ASCII value
/**
* C program to check vowel or consonant using ASCII values
*/
#include <stdio.h>
int main()
{
char ch;
/* Input 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);
}
else
{
/*
* If it is neither vowel nor consonant
* then it is not an alphabet.
*/
printf("%c is not an alphabet.", ch);
}
return 0;
}
Checking vowels and consonants using if...else
is simple. However, if…else statements are not recommended when checking condition with fixed constant values (vowels in this case). In such case switch...case
are recommended to use.
Learn – Program to check vowel and consonant using switch case.
Output
Enter any character: a 'a' is Vowel.
Happy coding 😉
Recommended posts
- If else programming exercise index.
- 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.