C program to check vowel or consonant using switch case

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.

Read more - Program to check vowel and consonant 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. Alphabets other than vowels are known as consonants. Below is the step by step descriptive logic to check vowel or consonant.

  1. Input a character from user, store it in some variable say ch.
  2. Switch the variable ch.
  3. Print "Vowel" for case 'a', 'e', 'i', 'o' and 'u' both lower and uppercase.
  4. Print "Consonant" if it is not vowel. Which is print "Consonant" for default case.

Let us code 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;

    /*
     * Input 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 can 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' to 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 printf("VOWEL") gets executed.

Output

Enter any alphabet: E
VOWEL

Happy coding 😉

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>