Write a c program to enter any alphabet and check whether alphabet is vowel or consonant using switch case. C program to check vowels and consonants using switch. Logic to check vowels and consonants using switch case.
Example
Input
Input alphabet: c
Output
'c' is consonant
Required knowledge
Basic programming, Switch case
Logic to check vowel or consonant using switch case
I already talked about vowels and consonants in one of previous program using if else.
In case you missed, here is a quick recap. English alphabets 'a', 'e', 'i', 'o', 'u' both lowercase and uppercase are known as vowels and alphabets other than vowels are known as consonants. Below is the step by step descriptive logic to check vowel or consonant.
- Read a character from user, store it in some variable say ch.
- Switch the variable ch.
- Print "Vowel" for the cases case 'a', case 'e', case 'i', case 'o' and case 'u' both lower and uppercase.
- Print "Consonant" if it is not vowel. Which is print "Consonant" for default case.
Let us code the solution of this program.
Program to check vowel and consonant
/** * C program to check vowel or consonant using switch case */ #include <stdio.h> int main() { char ch; /* * Read an alphabet from user */ printf("Enter any alphabet: "); scanf("%c", &ch); // Switch the variable ch switch(ch) { case 'a': printf("VOWEL"); break; case 'e': printf("VOWEL"); break; case 'i': printf("VOWEL"); break; case 'o': printf("VOWEL"); break; case 'u': printf("VOWEL"); break; case 'A': printf("VOWEL"); break; case 'E': printf("VOWEL"); break; case 'I': printf("VOWEL"); break; case 'O': printf("VOWEL"); break; case 'U': printf("VOWEL"); break; default: printf("CONSONANT"); } return 0; }
For a second observe the above program carefully. You will notice that I have repeated printf("VOWEL") for many cases. If a switch undergoes same task for various cases. Then you are free to remove the break statement and put all the repetitive statements in last repetitive case. Observe the below program.
Program to check vowel or consonant using switch case
/** * C program to check vowel or consonant using switch case */ #include <stdio.h> int main() { char ch; /* * Read an alphabet from user */ printf("Enter any character: "); scanf("%c", &ch); // Switch the character variable switch(ch) { case 'a': case 'e': case 'i': case 'o': case 'u': case 'A': case 'E': case 'I': case 'O': case 'U': // Print at last for all repetitions printf("VOWEL"); break; default: printf("CONSONANT"); } return 0; }
For the above program since there is no break statement for a, e, i, o, u, A, E, I, O cases. If it switches to any of the cases from case 'a' or case 'e' or ... or case 'U'. It will eventually fall down to last case (executing all statements between the falling cases) which contain break statement i.e. case 'U'. Hence, for a, e, i, o, u, A, E, I, O, U the printf("VOWEL") gets executed.
Enter any alphabet: E VOWEL
Happy coding ;)
You may also like
- Switch case programming exercise index
- C program to print day of week name using switch case.
- C program to find total number of days in a month using switch case.
- C program to find maximum between two numbers using switch case.
- C program to check whether a number is even or odd using switch case.
- C program to create simple calculator using switch case.