Write a C program to check whether an alphabet is a vowel or consonant
In this post, you will learn how to write a C program to check whether an alphabet is a vowel or a consonant.
The alphabet is made up of 26 letters, 5 of which are vowels (a, e, i, o, u) and the rest of which are consonants. There are different ways in C programming to check whether an alphabet is a vowel or consonant.
Using If Else statement
The given C program allows the user to enter any character and check whether the given alphabet is a vowel or consonant using the if else statement.
#include <stdio.h>
int main()
{
char ch;
printf("Please enter a character\n");
scanf("%c", &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 not a vowel.\n", ch);
return 0;
}
The character entered by the user is stored in a variable ch. Next, we have used the If Else statement to check whether the user entered character is equal to a, e, i, o, u, A, E, I, I, O. If it is TRUE, it is a vowel otherwise a consonant.
Output 1Please enter a character
A
A is a vowel.
Output 2
Please enter a character
g
g is not a vowel.
Using switch statement
In the given C program, we have used the switch statement to check whether the entered alphabet is consonant or vowel.
#include <stdio.h>
int main()
{
char ch;
printf("Enter an alphabet : ");
scanf("%c", &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 not a vowel.\n", ch);
}
return 0;
}
Output 1
Enter an alphabet : s
s is not a vowel.
Output 2
Enter an alphabet : I
I is a vowel.
Using user defined function
Here, we have checked whether a character is a consonant or not using a user-defined function. If it's not a vowel, then it is a consonant, but make sure that it's an alphabet, not a special character.
// C Program to check whether
// an alphabet is vowel or consonant
#include <stdio.h>
int check_vowel(char a);
int main()
{
char ch;
printf("Please enter an alphabet: \n");
scanf(" %c", &ch);
if(check_vowel(ch)) {
printf("%c is a vowel.", ch);
}
else {
printf("%c is a consonant.", ch);
}
return 0;
}
int check_vowel(char c)
{
if (c >= 'A' && c <= 'Z')
c = c + 32;
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
return 1;
return 0;
}
Output 1
Please enter an alphabet:
A
A is a vowel.
Output 2
Please enter an alphabet:
P
P is a consonant.
Related Articles
Prime factors of a number in cArmstrong number program in c
Write a program to check leap year in c
C program to find area of rectangle
C program to convert celsius to fahrenheit
Fibonacci series program in C using recursion
Write a program to find area of circle in C
C program to find greatest of three numbers
C program for addition of two numbers
C program to calculate compound interest
C program to find the ASCII value of a character
C program to convert Decimal to Octal
C program to convert decimal to binary
Write a C program to calculate Simple Interest
C program to check whether a number is even or odd
C program to reverse a number
C program to check palindrome number
Program to find square root of a number in C
C program to check whether a number is positive or negative