C program to check vowel or consonant using switch case

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.

Example
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.

  1. Input an alphabet from user. Store it in some variable say ch.
  2. Switch the value of ch.
  3. 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.
  4. Write all 10 possible cases for vowels and print “Vowel” for each case.
  5. If alphabet is not vowel then add a default case and print “Consonant”.

Learn – Program to check vowel and consonant using if…else.

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 😉