In this tutorial you will learn about the Java Program to Check Whether an Alphabet is Vowel or Consonant and its application with practical example.
Java Program to Check Whether an Alphabet is Vowel or Consonant
In this tutorial, we will learn to create a Java program that check whether the entered alphabet by the user is a vowel or a consonant using Java programming.
Prerequisites
Before starting with this tutorial, we assume that you are the best aware of the following Java programming topics:
- Operators in Java programming.
- Condition statement in Java programming.
- Logical operator in Java programming.
- Basic input/output in Java programming.
- Basic in Java programming.
What Is Vowel and consonant?
Vowel :=> Sound that allowing breath to flow out of mouth, without closing any part of the mouth is called as vowel.
Consonant:=> Sound made by blocking air from flowing out of the mouth with the teeth or throat is called as Consonant.
Example
As we know A E I O U are called vowels. And renaming alphabets except these 5 vowels are called consonants.
Program to Check the given value is Vowel or consonant.
So here we creating a program in which we will find the given character is vowel or consonant.
As we know a,e,i,o,u are only vowel and rest are consonant.So simply we will check these and if given character belongs to them then it is a vowel and if not its consonant simple isn’t it .let’s have a look at program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
import java.util.*; class Char { //Body of the main function of the program. public static void main(String[ ] arg) { //Declaring the required variables for the program. int i=0; char ch; //Creating the object of Scanner class Scanner sc=new Scanner(System.in); //Taking the input from the user for the program System.out.println("Enter a character : "); ch=sc.next( ).charAt(0); //char ch=sc.nextChar(); //Checking the character for the program switch(ch) { case 'a' : case 'e' : case 'i' : case 'o' : case 'u' : case 'A' : case 'E' : case 'I' : case 'O' : case 'U' :i++; } //If i value == 1; Then its a Vowel. if(i==1) //Printiong the character is a vowel System.out.println("Entered character "+ch+" is Vowel"); else if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')) //Printiong the character is a consonent. System.out.println("Entered character "+ch+" is Consonent"); else //Its not a valid alphabet System.out.println("Not an alphabet"); } } |
Output
Note: Here we assumes that the user will enter an alphabet. If not so then non-alphabetic character, it displays the character is neither a vowel nor a consonant.
In the above program, we have first declared a variables required in the program.
- ch= hold given character.
First of all user prompted to enter any character which will be assign in ch variable.
Then with the help of if-else statement we will check the inputted character is a vowel or consonant, using following logic
As user inputs any character we will check whether it’s a vowel both in lower-case and upper-case condition we check if belongs to in vowel or consonant. if it is vowel we will show following message.If a character doesn’t belong to this condition then it is consonant ,
and if doesn’t meet any of them vowel and consonant it might be a digit or a special symbol.
So in this tutorial we have learn to find given character is vowel or consonant.