C Program to check the alphabet is vowel or not
Objective
Write a C program to check if an entered alphabet is a Vowel or a Consonant.
Algorithm / Approach
- Declare a character variable.
- Use
scanf("%c", &a)to read a single character. - Use the logical OR operator (
||) to check if the character matches any lowercase vowel ('a', 'e', 'i', 'o', 'u'). - Use an
else ifto check against uppercase vowels ('A', 'E', 'I', 'O', 'U'). - If none match, print that it is NOT a vowel.
main.c
#include<stdio.h>
int main( ) {
int a, b,c;
printf("Enter Values for A : ");
scanf("%c",&a);
if(a=='a'||a=='e'||a=='I'||a='o'||a=='u') {
printf("Character is VOWEL\n");
}
else if(a=='A'||a=='E'||a=='I'||a='O'||a=='U') {
printf("Character is VOWEL\n");
}
else {
printf("Character is NOT VOWEL\n");
}
return 0;
}
Expected Output
Enter Values for A : x Character is NOT VOWEL
Explanation of the Program
- The Logical OR operator (
||) returns true if AT LEAST ONE of its conditions is true. - Note: You must compare the variable individually every time (e.g.,
a == 'a' || a == 'e'). You cannot writea == 'a' || 'e'. - A common bug in C is using the assignment operator (
=) instead of the equality operator (==) inside an if condition (e.g.,a = 'o'). This permanently changes the variable's value and always evaluates to true, breaking the logic.
Complexity
Time Complexity
O(1)
Space Complexity
O(1)
Common Mistakes
- Using a single equals sign
=instead of double equals==in the if-condition. The code provided actually contains this bug:a='o'anda='O'. This assigns the letter to the variable instead of comparing it!