codescracker
c

C Program to Check Vowel or Consonant



« Previous Program Next Program »

In this article, you will learn and get about checking whether the given character by user (at run-time) is a vowel or consonant. But before gets started, let's understand about vowel and consonant first.

Vowels and Consonants

There are 5 characters (a, e, i, o, u) from all the 26 characters of alphabet is known as vowel. List of all Vowels:

The first 5 characters are lowercase vowel and the second 5 characters are uppercase vowel. Except vowels, all the other 21 characters are known as consonant.

Check Vowel or Consonant in C

To check whether the input alphabet is a vowel or consonant in C programming, you have to ask from user to enter a character and check if the given character is equal to a, A, e, E, i, I, o, O, u, U or not. If it is equal to any one of these 10, then it is a vowel, otherwise it is a consonant. Let's have a look at the program:

// Check Vowel or Consonant in C
// ----codescracker.com----

#include<stdio.h>
#include<conio.h>
int main()
{
    char ch;
    printf("Enter an Alphabet: ");
    scanf("%c", &ch);
    if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u')
        printf("\nIt's a Vowel");
    else if(ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U')
        printf("\nIt's a Vowel");
    else
        printf("\nIt's a Consonant");
    getch();
    return 0;
}

This program is compiled and executed in Code::Blocks IDE. The snapshot given below shows the sample run above of this program:

c program check vowel

Now supply any alphabet (A-Z or a-z) say i and press ENTER key to see the output as shown in the snapshot given below:

program to check vowel c

Because the alphabet, i is a vowel, so you have seen the above output. Previous program is true only when user supply any input that comes under alphabet. But what if user enters any input as a special character say #. Because # is neither equal to lowercase vowel (a, e, i, o, u) nor equal to uppercase vowel (A, E, I, O, U). Therefore, else block gets executed and program will print the output It's a Consonant. And we all knows that # is a special character not a consonant. Therefore, let's modify this program with the program given below.

// Check Vowel or Consonant in C
// -----codescracker.com------

#include<stdio.h>
#include<conio.h>
int main()
{
    char ch;
    printf("Enter an Alphabet: ");
    scanf("%c", &ch);
    if((ch>='a' && ch<='z') || (ch>='A' && ch<='Z'))
    {
        if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u')
            printf("\nIt's a Vowel");
        else if(ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U')
            printf("\nIt's a Vowel");
        else
            printf("\nIt's a Consonant");
    }
    else
        printf("\nIt's neither Vowel nor Consonant");
    getch();
    return 0;
}

If you enters any alphabet, then this program will produce the same output as of first program given in this article. But if user enters any character other than an alphabet say 5, then this program will produce the output as given below:

check vowel or consonant c

In the above program, before checking for vowel or consonant. We have applied an extra code that checks whether the given character comes under a-z or A-Z or not. If it is an alphabet, then will continue to check for vowel or consonant, otherwise outer else block gets executed, and It's neither Vowel nor Consonant gets printed on output screen as shown in sample run given above.

Check Vowel or Not in C

It's a simple program that only checks for vowel. That is if user enters a vowel character, then this program will print it as vowel, otherwise It's not a Vowel gets printed.

// Check Vowel or Not in C
// -----codescracker.com------

#include<stdio.h>
#include<conio.h>
int main()
{
    char ch;
    printf("Enter an Alphabet: ");
    scanf("%c", &ch);
    if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u')
        printf("\nIt's a Vowel");
    else if(ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U')
        printf("\nIt's a Vowel");
    else
        printf("\nIt's not a Vowel");
    getch();
    return 0;
}

You can also write another program that works as same as above program. Here is the program:

// Check Vowel or Not in C
// -----codescracker.com------

#include<stdio.h>
#include<conio.h>
int main()
{
    char ch;
    int lowerVowel, upperVowel;
    printf("Enter an Alphabet: ");
    scanf("%c", &ch);
    lowerVowel = (ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u');
    upperVowel = (ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U');
    if(lowerVowel || upperVowel)
        printf("\nIt's a Vowel");
    else
        printf("\nIt's not a Vowel");
    getch();
    return 0;
}

Let's discuss about some steps used in above program:

Same Program in Other Languages


« Previous Program Next Program »