Write a C program to check if the entered character is a vowel or consonant using a switch statement.
In C programming, we can use a switch statement to check whether a given character is a vowel or consonant. The switch statement provides a clean and efficient way to evaluate multiple conditions. In this case, the program first asks the user to input a character. It then compares the character against the vowels ('a', 'e', 'i', 'o', 'u') in both uppercase and lowercase using a switch statement.
If the character matches any of these vowels, the program prints a message indicating that the character is a vowel. If there is no match, the default case of the switch is executed, and the program prints that the character is a consonant.
#include <stdio.h>
int main() {
char ch;
printf("Enter a letter: ");
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;
}
Output:
Enter a letter: d
d is a consonant
Switch statement in CMore Questions on Switch statement in C
Write a C program to find the day of the week based on an input number using a switch statement
View solution
Write a program in C to create a simple calculator using the switch statement.
View solution