C program to check vowel or consonant

Previous Program Next Program

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. Logic to check vowel or consonant in C program.

Example

Input

Input character: a

Output

'a' is vowel

Required knowledge

Basic C programming, If else

Logic to check vowels or consonants

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

Below is the step by step descriptive logic to check vowels or consonant.

  1. Read 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 the given character is not vowel then check conditions for consonant. If character is alphabet and it is not vowel then it is consonant. Means check ch >= 'a' and ch <= 'z', then it is consonant

Let us implement this 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;

    /* Reads a 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);
    }

    return 0;
} 

Note: You can also use ASCII character codes for checking vowels or consonants. Below program explains how to use ASCII character codes to check vowel and 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);


    // 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);
    }

    return 0;
} 

Also enhance your coding skills by learning this program using other approaches -

Output
Enter any character: a
a is VOWEL.

Happy coding ;)

You may also like

Previous Program Next Program
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
< pre>< code >----Your Source Code---- < / code > < / pre > (Remove spaces from pre and code tags)

No comments:

Post a Comment