C program to check if a given alphabet is vowel or consonant
Learn how to create a C program to determine whether an alphabet is a vowel or a consonant. Explore the code logic that distinguishes between vowels ('a', 'e', 'i', 'o', 'u') and consonants in the English alphabet. Enhance your C programming skills with this tutorial.
In this article, we'll explore how to create a simple C program to determine whether a given alphabet is a vowel or a consonant.
Understanding Vowels and Consonants
In the English alphabet, there are five vowels: 'a', 'e', 'i', 'o', and 'u'. All other alphabets are considered consonants.
C program to check if a given alphabet is vowel or consonant
Let's delve into the C programming language to create a program that checks whether a user-input alphabet is a vowel or a consonant.
#include <stdio.h>
int main() {
char ch;
// Input from the user
printf("Enter an alphabet: ");
scanf(" %c", &ch);
// Checking if the character is a vowel or a consonant
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
printf("%c is a vowel.\\n", ch);
} else if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
printf("%c is a consonant.\\n", ch);
} else {
printf("%c is not a valid alphabet.\\n", ch);
}
return 0;
}
Output
Enter an alphabet: a
a is a vowel.
Vowel or Consonant Check: The program checks if the entered character matches any of the vowels. If so, it's identified as a vowel. Otherwise, it checks if the character is an alphabet and not a vowel, indicating it's a consonant. Invalid input (non-alphabetic characters) are handled as well.
In this article, we've created a simple C program that efficiently determines whether a given alphabet is a vowel or a consonant. Understanding the English alphabet's vowel and consonant structure and using conditional checks in C enables us to perform accurate classifications.