Java program to identify whether the given character is a vowel or not. With the help of the below program, you will get to know how to write and print whether the given number is a vowel. We have written the program in three different ways, using if else statement, using switch case, user-defined method – learn more about switch case in Java with examples.
If you are good at programming even you can write the code in different formats. We are pretty sure that you can easily understand the following program. Just before that, we would like to share what are vowels and consonants in alphabets.
- What are the Vowels ?
A: Def: A speech sound which is produced with the vibration of vocal chords without audible friction, which is being blocked by teeth, tongue or lips :P.
We know that there are Five vowels: a, e, i, o, u or A, I, E, O, U.
- What are the consonants then?
Apart from A, E, I, O, U rest of the alphabets are consonants. That’s it.
Basically, we can write the code in two different ways, using if statement or if-else statement or Java switch case statement.
There you go:
Java Program To Print Whether The Given Alphabet is Vowel Or Consonant
1. Using Switch Case
Here we are using switch case, generally switch is used one out of multiple options, an if-else ladder can also be used to select one out of multiple options. In simple words, we can say the switch is a multi-branch statement.
Here the combination of both small letters and capital letters are represented as vowels. With the help of the case statement, the output will display on what the user is going to be entered. If it is out of 10 alphabets, it will display vowel or else consonant.
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 |
import java.util.Scanner; class Char { public static void main(String[ ] arg) { int i=0; Scanner sc=new Scanner(System.in); System.out.println("Enter a character : "); char ch=sc.next( ).charAt(0); //char ch=sc.nextChar(); 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==1) System.out.println("Entered character "+ch+" is Vowel"); else if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')) System.out.println("Entered character "+ch+" is Consonent"); else System.out.println("Not an alphabet"); } } |
1 2 3 |
Enter a character : a Entered character a is Vowel |
1 2 3 |
Enter a character : A Entered character A is Vowel |
1 2 3 |
Enter a character : Z Entered character Z is Consonant |
2. Using If else Statement
- There you go another program using if else statement with sample outputs – learn more about if else statement here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.Scanner; class Char { public static void main(String[ ] arg) { int i=0; Scanner sc=new Scanner(System.in); System.out.println("Enter a character : "); char ch=sc.next( ).charAt(0); if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U') { System.out.println("Entered character "+ch+" is Vowel"); } else if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')) System.out.println("Entered character "+ch+" is Consonant"); else System.out.println("Not an alphabet"); } } |
1 2 3 |
Enter a character : G Entered character G is Consonant |
1 2 3 |
Enter a character : 54 Not an alphabet |
3. Using Method
Java program to print whether the given character is vowel or consonant using Method or function with sample outputs.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.util.Scanner; class Char { void findVowelOrNot(char ch) { if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U') { System.out.println("Entered character "+ch+" is Vowel"); } else if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')) System.out.println("Entered character "+ch+" is Consonent"); else System.out.println("Not an alphabet"); } public static void main(String[ ] arg) { Char c=new Char(); Scanner sc=new Scanner(System.in); System.out.println("Enter a character : "); char in=sc.next( ).charAt(0); c.findVowelOrNot(in); } } |
1 2 3 |
Enter a character : E Entered character E is Vowel |
1 2 3 |
Enter a character : 6 Not an alphabet |