This program reads a character from the keyboard and, using if-else statements, determines whether it is a vowel, consonant, digit, or special character.
C Program
#include <stdio.h>
int main() {
char ch;
printf("Enter a character: ");
scanf(" %c", &ch);
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
char lower = (ch >= 'A' && ch <= 'Z') ? ch + 32 : ch;
if (lower == 'a' || lower == 'e' || lower == 'i' || lower == 'o' || lower == 'u') {
printf("'%c' is a vowel.\n", ch);
} else {
printf("'%c' is a consonant.\n", ch);
}
} else if (ch >= '0' && ch <= '9') {
printf("'%c' is a digit.\n", ch);
} else {
printf("'%c' is a special character.\n", ch);
}
return 0;
}
Explanation
- The first
ifchecks whether the character falls in the alphabet range (lowercase or uppercase) using ASCII range comparisons. - Uppercase letters are converted to lowercase by adding 32 (their ASCII offset), so the vowel check only needs to compare against
a, e, i, o, uonce. - The next
else ifchecks the ASCII digit range'0'to'9'. - Anything that's neither a letter nor a digit falls through to "special character" (e.g. punctuation, whitespace, symbols).
Sample Output
Enter a character: e
'e' is a vowel.
Enter a character: k
'k' is a consonant.
Enter a character: 7
'7' is a digit.
Enter a character: #
'#' is a special character.