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 for checking vowels and consonants using switch.
Also view this program using if else - C program to check whether an alphabet is vowel or consonant using if else.

Example:
Input any alphabet: c
Output: It is consonant.

Required knowledge:

Basic programming, Switch case

Program:

/**
 * C program to check vowel or consonant using switch case
 */

#include <stdio.h>

int main()
{
    char ch;

    /*
     * Reads an alphabet from user
     */
    printf("Enter any alphabet: ");
    scanf("%c", &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;
} 

Note: The above program can also be re-written in short as:

/**
 * C program to check vowel or consonant using switch case
 */

#include <stdio.h>

int main()
{
    char ch;

    /*
     * Reads an alphabet from user
     */
    printf("Enter any character: ");
    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("VOWEL");
            break;
        default: printf("CONSONANT");
    }

    return 0;
} 
Output
X
_
Enter any alphabet: E

VOWEL

Happy coding ;)

Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

No comments:

Post a Comment