Vowels program in java
Written by
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:
- Prompt the user to enter a character.
- Store the user's input in a variable.
- Create a duplicate of the original value in a different variable and change its case to lower case (or upper case, depending on the original case).
- Use an if-else construct to check if the character is a vowel.
- Compare the lower case (or upper case) version of the input character with all the lower case (or upper case) vowels.
- Print a message indicating whether the character is a vowel or not, based on the comparison.
Note: You can use both Scanner and BufferedReader Class to take input.
Source Code:
import java.util.*;
class CheckVowel {
public static void main() {
Scanner inp = new Scanner(System.in);
System.out.print("\nEnter 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.