if-else
statements and switch-case
statements.if-else
switch-case
a
, e
, i
, o
, u
(both uppercase and lowercase) are vowels.if-else
switch-case
if-else
#include <stdio.h>
void checkVowelOrConsonant(char ch) {
// Convert to lowercase for uniformity
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 if ((lower >= 'a' && lower <= 'z')) {
printf("%c is a consonant.\n", ch);
} else {
printf("%c is not an alphabet.\n", ch);
}
}
int main() {
char ch;
// Input from user
printf("Enter a character: ");
scanf(" %c", &ch);
checkVowelOrConsonant(ch);
return 0;
}
switch-case
#include <stdio.h>
void checkVowelOrConsonantSwitch(char ch) {
// Convert to lowercase for uniformity
char lower = (ch >= 'A' && ch <= 'Z') ? ch + 32 : ch;
switch (lower) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
printf("%c is a vowel.\n", ch);
break;
default:
if (lower >= 'a' && lower <= 'z') {
printf("%c is a consonant.\n", ch);
} else {
printf("%c is not an alphabet.\n", ch);
}
}
}
int main() {
char ch;
// Input from user
printf("Enter a character: ");
scanf(" %c", &ch);
checkVowelOrConsonantSwitch(ch);
return 0;
}
if-else
and switch-case
provides flexibility and understanding of decision-making constructs in C. The if-else
method is straightforward, while switch-case
is more structured and scalable for additional classifications.