C code to Check a Character is a Vowel or Consonant

Character is a vowel or consonant in C

Here, in this section we will discuss the program to check whether the character is a vowel or consonant in C.
Character is a vowel or consonant

Working:-

  • Take character input from the user
  • Check if Input is a lowercase of upper case vowel
  • If yes then print vowel
  • If not then print consonant
  • Can also additional check if it’s a non-character item

We will discuss various methods to do the same thing.

Method 1

Method 1

Run
// C Program to check whether alphabet is vowel or consonant
#include <stdio.h>

// main function
int main()
{
    char c='F';

    
        
    //checking for vowels	
    if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'||
    c=='A'||c=='E'||c=='I'||c=='O'||c=='U')
    {
        printf("%c is a vowel", c);  // condition true input is vowel
    }
    else
    {
        printf("%c is a consonant", c);  // condition true input is consonant
    }

    return 0;
}

Output

F is a consonant

Method 2

The issue with the previous method was that we were not checking if the user entered a non-alphabet character like ‘3’ or ‘%’ etc. We will see an alternate approach and also handle this non-alphabet case.

Method 2 (Code in C)

Run
#include <stdio.h>

int isLowercaseVowel(int c){
    // returns 1 if char matches any of below
    return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
}

int isUppercaseVowel(int c){
    // returns 1 if char matches any of below
    return (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');
}
int main() {
    char c='J';
    
    

    // show error message if c is not an alphabet
   
      
    if (isLowercaseVowel(c) || isUppercaseVowel(c))
        printf("%c is a vowel", c);  
        
    else
        printf("%c is a consonant", c);  

    return 0;
}

Output

J is a consonant

Method 3

The above method has two separate functions of upper case vowels and lowercase vowels we can reduce that down to one single method using an inbuilt function that converts any lowercase case charter to uppercase.

Method 2 (Code in C)

Run
#include <stdio.h>

// single function for both uppercase and lowercase
int isVowel(int c){
    // converts to uppercase if it wasn't already
    c = toupper(c);
    
    // returns true if char matches any of below
    return (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');
}
int main() {
    char c='i';
   

    // show error message if c is not an alphabet
    
      
   if (isVowel(c))
        printf("%c is a vowel", c);  
        
    else
        printf("%c is a consonant", c);

    return 0;
}

Output

i is a vowel

For similar questions click on given button

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

15 comments on “C code to Check a Character is a Vowel or Consonant”


  • Archisman

    #include
    int main()
    {
    char c;
    printf(“Enter an alphabet to check wheather it is vowel or consonant:”);
    scanf(“%c”,&c);

    if(c==’a’||c==’A’||c==’e’||c==’E’||c==’i’||c==’I’||c==’o’||c==’O’||c==’u’||c==’U’)
    {
    printf(“%c is vowel”,c);
    }

    else
    printf(“%c is consonant”,c);
    return 0;
    }


  • Shubhangi

    #include
    int main()
    {
    char c;
    char LowerVowel, UpperVowel;
    printf(“Enter an alphabet: “);
    scanf(“%c”,&c);

    //To find the corrector is lowercase vowel
    LowerVowel = (c == ‘a’ || c == ‘e’ || c == ‘i’ || c == ‘o’ || c == ‘u’);
    //To find the character is Upper case vowel
    UpperVowel = (c == ‘A’ || c == ‘E’ || c == ‘I’ || c == ‘O’ || c == ‘U’);
    // compare to charecter is Lowercase Vowel or Upper case Vowel

    if (LowerVowel || UpperVowel)
    printf(“%c is a vowel”, c);

    //to check character is alphabet or not
    else if(!((c >= ‘a’ && c = ‘A’ && c <= 'Z')))
    printf("%c not a alphabet", c);

    else
    printf("%c is a consonant", c);

    return 0;
    }


  • Vineeth

    I think this should be the code
    #include
    int main()
    {
    char c,lower, upper;
    printf(“Enter any character : “);
    scanf(“%c”,&c);

    //To find the corrector is lowercase vowel
    lower=(c==’a’||c==’e’||c==’i’||c==’o’||c==’u’);
    //To find the character is Upper case vowel
    upper=(c==’A’||c==’E’||c==’I’||c==’O’||c==’U’);

    if(lower||upper)
    printf(“It is a vowel”);

    //to check character is alphabet or not
    else if(c>=’z’ || c<='A')
    printf("It is not a alphabet");
    else
    printf("It is a consonant");
    return 0;
    }


  • Vineeth

    I think this should be the code
    #include
    int main()
    {
    char c,lower,upper;
    printf(“Enter any character : “);
    scanf(“%c”,&c);

    //To find the corrector is lowercase vowel
    lower=(c==’a’||c==’e’||c==’i’||c==’o’||c==’u’);

    //To find the character is Upper case vowel
    upper=(c==’A’||c==’E’||c==’I’||c==’O’||c==’U’);

    if(lower||upper)
    printf(“It is a vowel”);

    //to check character is alphabet or not
    else if(c>=’z’ || c<='A')
    printf("It is not a alphabet");
    else
    printf("It is a consonant");
    return 0;
    }


  • nischayjain07

    #include
    int main(){
    char c;
    scanf(“%c”, &c);
    if((c>=’a’&&c=’A’&&c<='Z'))
    {
    if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u')
    {
    printf("%c is a vowel\n", c);
    }
    else if (c=='A'||c=='E'||c=='I'||c=='O'||c=='U')
    {
    printf("%c is a vowel\n", c);
    }
    else printf("%c is a consonant\n", c);
    }
    else printf("%c is an invalid entry\n", c);
    return 0;
    }


  • Abhimanyu

    Right Solution
    else if((c =’z’ )|| (c>=’A’ && c <= 'Z'))
    printf(" is not A Alphabet \n ");


  • ankita

    #include
    int main()
    {
    char c;
    printf(“enter the alphabet:”);
    scanf(“%c”,&c);
    if(c==’a’ || c==’e’ || c==’i’ || c==’o’ || c==’u’ || c==’A’ || c==’E’ || c==’I’ || c==’O’ || c==’U’)
    {
    printf(“given alphabet is vowel”);
    }
    else
    {
    printf(“given alphabet is consonent”);
    }

    }


  • Ruby

    if (isLowerVowel || isUpperVowel)
    printf(“%c is a vowel”, c);
    //to check character is alphabet or not

    else if((c >= ‘a’|| c>=’A’) &&( c <= 'z' || c <= 'Z'))
    printf("%c is a consonant",c);
    else
    printf("%c is not a alphabet", c);


  • sachin

    #include
    int main()
    {
    int lower,upper;
    char ch;
    printf(“ente the character”);
    scanf(“%c”,&ch);
    lower=(ch==’a’|| ch==’e’|| ch==’i’ || ch==’o’ || ch==’u’);
    upper=(ch==’A’|| ch==’E’|| ch==’I’ || ch==’O’ || ch==’U’);
    if(lower || upper)
    {
    printf(“is vowel”);
    }
    else if((ch>=’a’ && ch=’A’ && ch<='Z'))
    {
    printf("is consonant");

    }
    else
    {
    printf("not an alphabet");
    }
    return 0;
    }


  • sachin

    THIS IS BETTER

    #include
    int main()
    {
    int lower,upper;
    char ch;
    printf(“ente the character”);
    scanf(“%c”,&ch);
    lower=(ch==’a’|| ch==’e’|| ch==’i’ || ch==’o’ || ch==’u’);
    upper=(ch==’A’|| ch==’E’|| ch==’I’ || ch==’O’ || ch==’U’);
    if(lower || upper)
    {
    printf(“is vowel”);
    }
    else if((ch>=’a’ && ch=’A’ && ch<='Z'))
    {
    printf("is consonant");

    }
    else
    {
    printf("not an alphabet");
    }
    return 0;
    }