Write a C program to input an alphabet and check whether it is vowel or consonant using switch case. C program to check vowel or consonant using switch case. Logic to check vowel or consonant using switch case.
Input
Input alphabet: c
Output
'c' is consonant
Required knowledge
Basic C programming, Switch case statement
Logic to check vowel or consonant using switch...case
statement
English alphabets ‘a’, ‘e’, ‘i’, ‘o’, ‘u’ both lowercase and uppercase are known as vowels. Alphabets other than vowels are known as consonants.
Step by step descriptive logic to check vowel or consonant.
- Input an alphabet from user. Store it in some variable say ch.
- Switch the value of ch.
- For ch, there are 10 possibilities for vowel we need to check i.e. a, e, i, o, u, A, E, I, O and U.
- Write all 10 possible cases for vowels and print “Vowel” for each
case
. - If alphabet is not vowel then add a
default
case and print “Consonant”.
Program to check vowel or consonant using switch...case
statement
/**
* C program to check vowel or consonant using switch case
*/
#include <stdio.h>
int main()
{
char ch;
/* Input an alphabet from user */
printf("Enter any alphabet: ");
scanf("%c", &ch);
/* Switch value of 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;
}
Logic to check vowel or consonant using switch...case
statement
Observe the above program carefully. I have repeated printf("Vowel");
for many cases.
I already explained in my previous post to print number of days in months, if a switch...case
contains same action for multiple cases. Then group all cases performing same action together. Arrange all similar cases together and remove break
statement from all similar cases other than the last case.
So you can group all cases to print vowel together as.
switch(ch)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
printf("Vowel");
break;
}
In above code, if program control switches to any of the case in a, e, i, o, u, A, E, I, O, U it executes all below statements till break
is found. Hence, for any of the case in a, e, i, o, u, A, E, I, O, U it prints “Vowel”.
Program to check vowel or consonant using switch...case
statement
/**
* C program to check vowel or consonant using switch case
*/
#include <stdio.h>
int main()
{
char ch;
/* Input alphabet from user */
printf("Enter any character: ");
scanf("%c", &ch);
/* Switch ch value */
switch(ch)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
printf("Vowel");
break;
default: printf("Consonant");
}
return 0;
}
Output
Enter any alphabet: E VOWEL
Happy coding 😉
Recommended posts
- 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.