- C Programming Examples
- C Programming Examples
- C Print Hello World
- C Get Input from User
- C Print Integer
- C Add Two Numbers
- C Add Subtract Multiply Divide
- C Add n Numbers
- C Area Perimeter of Square
- C Area Perimeter of Rectangle
- C Area Circum of Circle
- C Fahrenheit to Celsius
- C Celsius to Fahrenheit
- C Inches to Centimeters
- C Kilogram to Gram
- C Reverse a Number
- C Swap Two Numbers
- C Interchange Numbers
- C Print ASCII Value
- C Print Fibonacci Series
- C Check Palindrome or Not
- C Check Armstrong or Not
- C Find Armstrong Numbers
- C Find nCr and nPr
- C Find Profit Loss
- C Sum of their Square
- C First & Last Digit Sum
- C Sum of All Digit
- C Product of All Digit
- C Print Total Digit in Number
- C Check Perfect Number
- C Find Basic Gross Salary
- C Round Number to Integer
- C Print Series upto n Term
- C Find Factors of Number
- C if-else & Loop Programs
- C Check Even or Odd
- C Check Prime or Not
- C Check Alphabet or Not
- C Check Vowel or Not
- C Check Leap Year or Not
- C Is Reverse Equal Original
- C Make Calculator
- C Add Digits of Number
- Count Positive Negative Zero
- C Largest of Two Numbers
- C Largest of Three Numbers
- C Smallest of Two Numbers
- C Smallest of Three Numbers
- C Find Factorial of Number
- C Find LCM & HCF
- C Find LCM of n Numbers
- C Find HCF of n Numbers
- C Find Arithmetic Mean
- C Find Average, Percentage
- C Find Student Grade
- C Print Table of Number
- C Print Prime Numbers
- C Find Discount Purchase
- C Calculate Parcel Charge
- C Calculate Wage of Labor
- C Print Phone Bill
- C Conversion programs
- C Decimal to Binary
- C Decimal to Octal
- C Decimal to Hexadecimal
- C Binary to Decimal
- C Binary to Octal
- C Binary to Hexadecimal
- C Octal to Decimal
- C Octal to Binary
- C Octal to Hexadecimal
- C Hexadecimal to Decimal
- C Hexadecimal to Binary
- C Hexadecimal to Octal
- C Pattern Programs
- C Pattern Printing Programs
- C Print Diamond Pattern
- C Print Floyd's Triangle
- C Print Pascal's Triangle
- C Array Programs
- C 1D Array Programs
- C Linear Search
- C Binary Search
- C Largest Element in Array
- C Smallest Element in Array
- C Second Largest/Smallest
- C Count Even Odd
- C Array Element at Even
- C Array Element at Odd
- C Print Even Array Elements
- C Print Odd Array Elements
- C Sum/Product of Even/Odd
- C Reverse an Array
- C Insert Element in Array
- C Delete Element from Array
- C Merge Two Arrays
- C Bubble Sort
- C Selection Sort
- C Insertion Sort
- C Print Common Elements
- C 2D Array Programs
- C Add Two Matrices
- C Subtract Two Matrices
- C Transpose a Matrix
- C Multiply Two Matrices
- C Sum All Matrix Elements
- C Largest Element in Matrix
- C Print Row Column Total
- C 3D Array Programs
- C String Programs
- C Print String
- C Find Length of String
- C Compare Two String
- C Copy a String
- C Concatenate String
- C Reverse a String
- C Count Vowels Consonants
- C Replace Vowel in String
- C Delete Vowels from String
- C Delete Word from String
- C Frequency of Character
- C Count Word in String
- C Remove Spaces from String
- C Sort a String
- C Sort String in Alphabetical
- C Sort Words in Ascending
- C Sort Words in Descending
- C Uppercase to Lowercase
- C Lowercase to Uppercase
- C Swap Two Strings
- C Check Anagram or Not
- C Check Palindrome String
- C Print Number in Words
- C Print Successive Character
- C Character without Space
- C File Programs
- C Read a File
- C Write Content to File
- C Read & Display File
- C Copy a File
- C Merge Two Files
- C Reverse File
- C Count All Character in File
- C List Files in Directory
- C Encrypt & Decrypt a File
- C Delete a File
- C Misc Programs
- Generate Random Numbers
- C Print Date Time
- C Print Message with Time
- C Get IP Address
- C Print Smiling face
- C Pass Array to Function
- Add Two Numbers using Pointer
- C Address of Variable
- C Shutdown Computer
- C Programming Tutorial
- C Tutorial
C Program to Check Vowel or Consonant
In this article, you will learn how to check whether the character given by the user (at runtime) is a vowel or a consonant. But before we get started, let's understand what a vowel and a consonant are.
Vowels and consonants
There are 5 characters (a, e, i, o, u) from all the 26 characters of the alphabet that are known as vowels. Vowels in alphabetical order:
- a
- e
- i
- o
- u
- A
- E
- I
- O
- U
The first five characters are lowercase vowels, and the second five characters are uppercase vowels. Except for vowels, all the other 21 characters are known as consonants.
In C, check for a vowel or a consonant
To check whether the input alphabet is a vowel or consonant in C programming, you have to ask the user to enter a character and check if the given character is equal to a, A, e, E, i, I, o, O, u, U, or not. If it is equal to any one of these 10, then it is a vowel; otherwise, it is a consonant. Let's have a look at the program:
#include<stdio.h> #include<conio.h> int main() { char ch; printf("Enter an Alphabet: "); scanf("%c", &ch); if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u') printf("\nIt's a Vowel"); else if(ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U') printf("\nIt's a Vowel"); else printf("\nIt's a Consonant"); getch(); return 0; }
This program is compiled and executed in the Code::Blocks IDE. The snapshot given below shows the sample run above of this program:
Now supply any alphabet (A-Z or a-z), say "i," and press the ENTERÂ key to see the output as shown in the snapshot given below:
Because i is a vowel in the alphabet, so you have seen the above output. The previous program is true only when the user supplies any input that comes
under the alphabet.But what if user enters any input as a special character, say #?
because # is neither equal to a lowercase vowel
(a, e, i, o, u) nor equal to an uppercase vowel (A, E, I, O, U).
As a result, the else block gets executed, the program will print the output It's a Consonant. And we all knows that # is a special character, not a consonant. Therefore, let's modify this program to match the one given below.
#include<stdio.h> #include<conio.h> int main() { char ch; printf("Enter an Alphabet: "); scanf("%c", &ch); if((ch>='a' && ch<='z') || (ch>='A' && ch<='Z')) { if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u') printf("\nIt's a Vowel"); else if(ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U') printf("\nIt's a Vowel"); else printf("\nIt's a Consonant"); } else printf("\nIt's neither Vowel nor Consonant"); getch(); return 0; }
If you enter any letter, then this program will produce the same output as the first program given in this article. But if the user enters any character other than an alphabet, say 5, then this program will produce the output as given below:
In the above program, before checking for a vowel or consonant, We have applied an extra code that checks whether the given character falls under a-z or A-Z or not. If it is an alphabet, then it will continue to check for a vowel or consonant; otherwise, the outer else block gets executed, and It's neither Vowel nor Consonant gets printed on the output screen as shown in the sample run given above.
Check if the given alphabet is a vowel
It's a simple program that only checks for vowels. That is, if the user enters a vowel character, then this program will print it as a vowel; otherwise, It's not a Vowel gets printed.
#include<stdio.h> #include<conio.h> int main() { char ch; printf("Enter an Alphabet: "); scanf("%c", &ch); if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u') printf("\nIt's a Vowel"); else if(ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U') printf("\nIt's a Vowel"); else printf("\nIt's not a Vowel"); getch(); return 0; }
You can also create another program that performs the same functions as the one described above. Here is the program:
#include<stdio.h> #include<conio.h> int main() { char ch; int lowerVowel, upperVowel; printf("Enter an Alphabet: "); scanf("%c", &ch); lowerVowel = (ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u'); upperVowel = (ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U'); if(lowerVowel || upperVowel) printf("\nIt's a Vowel"); else printf("\nIt's not a Vowel"); getch(); return 0; }
Let's go over some of the steps in the preceding program:
- Scan a character input into the ch variable.
- If ch is equal to any of the lowercase vowels (a, e, i, o, u). Then 1 is set to lowerVowel.
- Or if ch is equal to any of the uppercase vowels (A, E, I, O, U). Then again, 1 is set to upperVowel.
- Now use the if-else case to check whether lowerVowel or upperVowel contains 1 or not.
- If either of the two variables, lowerVowel and upperVowel, contains a 1, the condition of the if block evaluates to true, and It's a Vowel gets printed.
- Otherwise, if both variables, say lowerVowel and upperVowel, contains 0. And because 0 is considered false, the condition of the if block evaluates to false, and the else block is executed, and It's not a Vowel gets printed on the output console.
The same program in different languages
« Previous Program Next Program »