Lets write a C program to check whether user entered character is a vowel or a consonant.
Related Read:
if else statement in C
Logical Operators In C
Note: Lowercase English alphabets a, e, i, o, u and uppercase English alphabets A, E, I, O, U are called Vowels. All other alphabets are called Consonants.
We assume that the user enters only alphabets as input for this program.
Source Code: C Program To Check Whether a Character is Vowel or Consonant
#include < stdio.h > int main() { char ch; printf("Enter a character\n"); scanf("%c", &ch); if( ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') { printf("%c is vowel\n", ch); } else { printf("%c is consonant\n", ch); } return 0; }
Output 1:
Enter a character
A
A is vowel
Output 2:
Enter a character
B
B is consonant
Output 3:
Enter a character
e
e is vowel
Output 4:
Enter a character
f
f is consonant
Logic To Check Whether a Character is Vowel or Consonant
We ask the user to enter an alphabet and store it inside character variable ch. Using if else construct we check if the user entered alphabet is vowel or consonant. Inside if condition, we check if alphabet present in variable ch is equal to a or e or i or o or u or A or E or I or O or U. If any of these conditions are true, then it’ll return true and block inside if executes orelse else block gets executed.
In the background, c program checks the ASCII value present in variable ch with the ASCII values of a, e, i, o, u, A, E, I, O, U.
Video Tutorial: C Program To Check Whether a Character is Vowel or Consonant
[youtube https://www.youtube.com/watch?v=lJSmUKw3cMM]
For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List
For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert