C program to check vowel or consonant

Write a C program to check whether an alphabet is vowel or consonant using if else. How to check vowels and consonants using if else in C programming. Program to input a character from user and check whether it is vowel or consonant in C.


Example:
Input character: a
Output: 'a' is vowel.

Required knowledge

Basic C programming, If else

Logic to check vowels or consonants

We know that English alphabets 'a', 'e', 'i', 'o', 'u' both lowercase and uppercase are known as vowels and alphabets other than vowels are known as consonants.
In C programming to check if a character is vowel, you just need to check if it is one of the characters a e i o u or A E I O U.
If it is not vowel and entered character is an alphabet i.e. it is in range of a-z or A-Z then it is consonant.

Program to check vowel or consonant

/**
 * C program to check whether a character is vowel or consonant
 */

#include <stdio.h>

int main()
{
    char ch;

    /* Reads a character from user */
    printf("Enter any character: ");
    scanf("%c", &ch);


    if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' || ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U')
    {
        printf("%c is VOWEL.\n", ch);
    }
    else if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
    {
        printf("%c is CONSONANT.\n", ch);
    }

    return 0;
} 


Note: You can also use ASCII character codes for checking vowels or consonants.


Program to check vowel or consonant using ASCII values

/**
 * C program to check vowel or consonant using ASCII values 
 */

#include <stdio.h>

int main()
{
    char ch;

    /* Reads any character from user */
    printf("Enter any character: ");
    scanf("%c", &ch);


    if(ch==97 || ch==101 || ch==105 || ch==111 || ch==117 || ch==65 || ch==69 || ch==73 || ch==79 || ch==85)
    {
        printf("%c is VOWEL.\n", ch);
    }
    else if((ch >= 97 && ch <= 122) || (ch >= 65 && ch <= 90))
    {
        printf("%c is CONSONANT.\n", ch);
    }

    return 0;
} 


Also enhance your coding skills by learning this program using switch case.


Output
Enter any character: a
a is 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