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. C Program to input a character from user and check whether it is vowel or consonant. Logic to check vowel or consonant in C programming.

Example
Input

Input character: a

Output

'a' is vowel

Required knowledge

Basic C programming, Relational operators, Logical operators, If else

Logic to check vowels or consonants

English alphabets a, e, i, o and u both lowercase and uppercase are known as vowels. Alphabets other than vowels are known as consonants.

Step by step descriptive logic to check vowels or consonant.

  1. Input a character from user. Store it in some variable say ch.
  2. Check conditions for vowel i.e. if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u'), then it is vowel.
  3. If character is alphabet but not vowel then it is consonant. Means check ch >= 'a' && ch <= 'z' then, it is consonant.
  4. If it is neither vowel nor consonant, then it is not alphabet.

Character in C is represented inside single quote. Do not forget to add single quote whenever checking for character constant.

Let us implement the logic with both lower and upper case alphabets.

Program to check vowel or consonant

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

#include <stdio.h>

int main()
{
    char ch;

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


    /* Condition for vowel */
    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.", ch);
    }
    else if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
    {
        /* Condition for consonant */
        printf("'%c' is Consonant.", ch);
    }
    else 
    {
        /*
         * If it is neither vowel nor consonant
         * then it is not an alphabet.
         */
        printf("'%c' is not an alphabet.", ch);
    }

    return 0;
}

Logic to check vowel or consonant using ASCII value

Internally characters in C is represented by an integer value known as ASCII value. You can also use ASCII value of a character to check vowels and consonants.

ASCII value of A=65, E=69 ... a=97, e=102 etc. So, instead of checking condition if(ch=='a' || ch=='e' || ... ) you can use if(ch==97 || ch==102 || ...)

Program to check vowel or consonant using ASCII value

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

#include <stdio.h>

int main()
{
    char ch;

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


    /* Condition for vowel */
    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.", ch);
    }
    else if((ch >= 97 && ch <= 122) || (ch >= 65 && ch <= 90))
    {
        /* Condition for consonant */
        printf("'%c' is Consonant.", ch);
    }
    else 
    {
        /*
         * If it is neither vowel nor consonant
         * then it is not an alphabet.
         */
        printf("%c is not an alphabet.", ch);
    }

    return 0;
}

Checking vowels and consonants using if...else is simple. However, if...else statements are not recommended when checking condition with fixed constant values (vowels in this case). In such case switch...case are recommended to use.

Learn - Program to check vowel and consonant using switch case.

Output

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