Java Program to check vowel or consonant using if-else
In the following problem, we are asked to write a Java source code where we are supposed to ask the user to enter a character using standard input and then check if the character is a vowel or not.
The algorithm will be:
- Enter and Store User Input.
- Create a duplicate of the original value in a different variable and change its case.
- Use an if-else construct.
- Compare input character with all the vowels (both in Lower Case and Upper Case).
- Print the result accordingly.
Note: You can use both Scanner and BufferedReader Class to take input.
Source Code:
/* Program to check if entered character is vowel or not using if-else*/
import java.util.*;
class CheckVowel
{
public static void main()
{
Scanner inp=new Scanner(System.in);
System.out.print("\n Enter Character: ");
char c=((inp.nextLine()).charAt(0));
char z=Character.toUpperCase(c); //Changing Value to UpperCase for uniformity.
if(z=='A' || z=='E' || z=='I' || z=='O' || z=='U') //Checking if Vowel
System.out.println(c+" is a Vowel.");
else
System.out.println(c+" is not a Vowel.");
}
}
Output:
Enter Character: t
t is not a Vowel.
Enter Character: V
V is not a Vowel.
Enter Character: o
o is a Vowel.
Enter Character: A
A is a Vowel.
Report Error/ Suggestion