Vowel or Consonant Using If-Else
#include <stdio.h>
int main() {
char ch;
printf("Enter an alphabet: ");
scanf(" %c", &ch);
if (ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||
ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U') {
printf("%c is a vowel\n", ch);
} else {
printf("%c is a consonant\n", ch);
}
return 0;
}
Enter an alphabet: e
e is a vowel
This method compares the input character against all vowels (both lowercase and uppercase). If none match, the character is considered a consonant.
Vowel or Consonant Using Switch
#include <stdio.h>
int main() {
char ch;
printf("Enter an alphabet: ");
scanf(" %c", &ch);
switch(ch) {
case 'a': case 'e': case 'i': case 'o': case 'u':
case 'A': case 'E': case 'I': case 'O': case 'U':
printf("%c is a vowel\n", ch);
break;
default:
printf("%c is a consonant\n", ch);
}
return 0;
}
Enter an alphabet: x
x is a consonant
A switch statement can simplify multi-branch checks. By grouping vowel cases together, the program prints a message when any vowel is matched and uses the default branch for consonants.
