C program to check vowel

In this C programming article we will learn to check vowel whether a given character vowel or consonant. First we will take a character input from user and then check it that the user has given a vowel or consonant. We can do this by several logic. Let’s see some of them bellow.

Check vowel using if else statement

You can apply different logic to check a character that it is vowel or consonant by if else statement. Some of them are like this.

#include <stdio.h>
int main(){
    char myChar;
    printf("Enter a character to check : ");
    scanf("%c", &myChar);

    if(myChar == 'a' || myChar == 'e' || myChar == 'i' || myChar == 'o' || myChar == 'u' ||
       myChar == 'A' || myChar == 'E' || myChar == 'I' || myChar == 'O' || myChar == 'u'){
        printf("\n\t%c is vowel.\n", myChar);
    }else{
        printf("\n\t%c is consonant.\n", myChar);
    }
return 0;
}

We can also write this program more efficiently as following.

#include <stdio.h>
int main(){
  char myChar;

  printf("Input a character to check : ");
  scanf("%c", &myChar);

  if ((myChar >= 'a' && myChar <= 'z') || (myChar >= 'A' && myChar <= 'Z')) {
    if (myChar =='a' || myChar =='A' || myChar =='e' || myChar=='E' || myChar=='i' || myChar=='I'
        || myChar=='o' || myChar=='O' || myChar== 'u' || myChar=='U'){
      printf("\n%c is a vowel.\n", myChar);
    }else{
      printf("\n%c is a consonant.\n", myChar);
    }
  }else{
  printf("\n%c is neither a consonant nor a vowel.\n", myChar);
  }
  return 0;
}

Output of this check vowel program

check vowel, checking vowel or consonant in c

Check vowel using switch statement

#include <stdio.h>

int main(){
  char myChar;

  printf("Input a character here : ");
  scanf("%c", &myChar);

  switch(myChar){      // switch statement
    case 'a':
    case 'e':
    case 'i':
    case 'o':
    case 'u':
    case 'A':
    case 'E':
    case 'I':
    case 'O':
    case 'U':
      printf("\n%c is a vowel.\n", myChar);
      break;

    default:     // It may be vowel or other character
      printf("\n%c is not a vowel.\n", myChar);
  }

  return 0;
}

Output of program:

checking vowel or consonant using function, checking vowel by switch

Check vowel or consonant using function

#include <stdio.h>

int isVowel(char x){       // function to check vowel
  if (x == 'A' || x == 'E' || x == 'I' || x == 'O' || x == 'U'){
    return 1;
  }else if (x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u'){
    return 1;
  }else{
    return 0;
  }
}
int main(){
    char x;
    printf("Enter the character you want to check : ");
    scanf("%c", &x);

    if(isVowel(x)){      // calling function inside if condition
        printf("\nVowel\n");
    }else{
        printf("\nNot a vowel\n");
    }
return 0;
}

See the output here

Enter the character you want to check : u

Vowel

Process returned 0 (0x0)   execution time : 5.044 s
Press any key to continue.

Previous pageMultiply and divide program

Next pageLetter grade program

More C programming and projects

C learning topics