Program to Check Whether a Character is a Vowel or Consonant in C

Vowel or Consonant:

On this page we will write a program to check whether a character is a vowel or consonant in C language.Out of 26 Alphabets there are total 5 vowel and  rest 21 are  consonants.So, this program is important to understand the basics of ‘char’ data type an basic structure of C programming.

Check Whether a Character is a Vowel or Consonant in C

Check Whether a Character is a Vowel or Consonant in C language:

There are 26 alphabets in total out of which 5 are vowel(a,e,i,o,u) rest are consonant.We can find where a character is vowel or not using if else or switch case.It is one of the simple and Important program to understand and learn the basic of coding.We have to consider both lowercase and uppercase alphabets in this program .

Program 1:

Run
#include <stdio.h>
int main ()
{
  char a = 'e';


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

Output:

e is a vowel.

Program 2:

Run
#include <stdio.h>
int main ()
{
  char a = 'p';
  int lc, uc;

  lc = (a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u');

  uc = (a == 'A' || a == 'E' || a == 'I' || a == 'O' || a == 'U');


  if (lc || uc)
    printf ("%c is a vowel.", a);
  else
    printf ("%c is a consonant.", a);
  return 0;
}

Output:

p is a consonant.

There is one drawback in above method if user enter number as a character in that case the program will give wrong answer.So, to overcome that we will use isalpha( ) functio which checks whether the given character is alphabet or number.

Program 3:

Run
#include <ctype.h>
#include <stdio.h>

int main ()
{
  char a = '7';

  if (!isalpha (a))
    printf ("Error! Non-alphabetic character.");
  else if (a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u'
	   || a == 'A' || a == 'E' || a == 'I' || a == 'O' || a == 'U')
    printf ("%c is a vowel.", a);
  else
    printf ("%c is a consonant.", a);


  return 0;
}

Output:

Error! Non-alphabetic character.

Prime Course Trailer

Related Banners

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

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription