In this C programming example, if...elseif...else statement is used to check whether an alphabet entered by the user is an alphabet or not and if alphabet, whether vowel or a constant. It check the entered character for both lowercase and uppercase alphabets. If user enters input other than a character, it displays appropriate output message.
C program to check vowel or consonant - Source code
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;
}
Program Output
Case 1:
Enter any character: i
'i' is Vowel.
Case 2:
Enter any character: F
'F' is Consonant.
Program Explanation
1. Input a character from user. Store it in the character variable ch.
2. Check condition for being the vowel i.e. if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u'
C program to check vowel or consonant - Source code
Program Output
Program Explanation
1. Input a character from user. Store it in the character variable ch.
2. Check condition for being the vowel i.e. if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u'
ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U').
3. Check for the consonant using the expression ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')).
4. In the last else statement , If it is neither vowel nor consonant, then it is not alphabet or some other character.
5. Characters in C are represented inside single quotes.
need an explanation for this answer? contact us directly to get an explanation for this answer