C Program to check whether an alphabet is a vowel or consonant

In this C Programming example, we will implement the C Program to check whether an alphabet is a vowel or consonant using conditional statements and logical operators.

1. What is a Vowel or a Consonant?

Given a character (ch), we need to find if the character entered by the user is a vowel or a consonant. If the character entered is "A", "a","E","e","I","i","O","o","U","u", then it is a “Vowel” otherwise, it is a “Consonant“.

Enter a character: A
Output : Is a Vowel.

Enter a character: z
Output : Is a Consonant.

Helpful topics to understand this program better are-


2. C Program to check if the alphabet is a vowel or a consonant

Let’s discuss the execution(kind of pseudo-code) for the program to check if an alphabet entered is a vowel or a consonant.

2.1. Using if-else conditions

In this section, we will discuss how to check if an alphabet entered by the user is a vowel or a consonant and implement the C program using if-else.

  1. The user enters the character to check which is stored in the variable ch.
  2. The character is passed to the method checkIfVowelOrConsonant(char ch) which contains logic to test whether the character is a vowel or a consonant
  3. If the character is found to be a vowel then print “Is a Vowel” otherwise, print “Is a Consonant.”
#include <stdio.h>

void checkIfVowelOrConsonant(char ch) {
  if (ch == 'a' || ch == 'A' || 
      ch == 'e' || ch == 'E' || 
      ch == 'i' || ch == 'I' ||
      ch == 'o' || ch == 'O' ||
      ch == 'u' || ch == 'U')
    printf("%c Is a vowel.\n", ch);
  else
    printf("%c Is a consonant.\n", ch);
}
int main() {
  char ch;
  printf("Enter a character\n");
  scanf("%c", &ch);

  checkIfVowelOrConsonant(ch);
  return 0;
}
Output
Enter a character
s
s is a consonant.

2.2. Using Switch statement

Another approach we can take to find whether a character is a vowel or a consonant is to use a Switch Statement. Let’s implement the code using a switch case.

Note: We will just update the method checkIfVowelOrConsonant(char ch) as the main method remains the same.

void checkIfVowelOrConsonant(char ch) {
  switch (ch) {
  case 'a':
  case 'A':
  case 'e':
  case 'E':
  case 'i':
  case 'I':
  case 'o':
  case 'O':
  case 'u':
  case 'U':
    printf("%c is a vowel.\n", ch);
    break;
  default:
    printf("%c is a Consonant.\n", ch);
  }
}
Enter a character
O
O is a vowel.

3. Conclusion

In this C Programming example, we have discussed how to check if an alphabet entered is a vowel or a consonant using the logical operator and conditional statements.


Helpful Links

Please follow C Programming tutorials or the menu in the sidebar for the complete tutorial series.

Also for the example C programs please refer to C Programming Examples.

All examples are hosted on Github.


Recommended Books


An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding

Do not forget to share and Subscribe.

Happy coding!! ?

Recommended -

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Index