The characters A, E, I, O, U are called vowels. The rest of the characters are called consonants. Here we are writing a java program to check whether a character is a vowel or consonant.
To check a character is a vowel or consonant in java, first, we have to declare the class ConsonentVowel.Then read a character ch from the user. Then by using a if
statement checks the character is a vowel, both small and capital are given in condition ie., a, e, i, o, u, A, E, I, O, U. If equals then display the character is a vowel, else display the character is consonant.
For reading a character from the user in java we can use the next() function in the java Scanner class. The next().chartAt(0) reads the first character of the string which is entered by the user.
STEP 1: Declare the class ConsonentVowel with a public modifier.
STEP 2: Open the main() to start the program, Java program execution starts with the main()
STEP 3: Display the user to enter a character.
STEP 4: Read the character from the user using scanner.next().charAt(0) into the variable ch.
STEP 5: By using the if
condition check the ch equals a or e or i or o or u or A or E or I or O or U
STEP 6: If the condition is true then display the character as a vowel.
STEP 7: If the condition is false then display the character is consonant.
import java.util.Scanner;
public class ConsonentVowel {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.println("Enter a character : ");//Read a character from user
char ch=scanner.next().charAt(0);
scanner.close();
if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U' )//Check both uppercase and lowercase
System.out.println(ch + " is a Vowel");//If ch is vowel then display Vowel
else
System.out.println(ch + " is a Consonant");
}
}
OUTPUT 1 Enter a character : a a is a Vowel OUTPUT 2 Enter a character : b b is a Consonant