In this tutorial, you will be learning to write Java Program to check whether the given character is a vowel or consonant.
There are total 5 vowels in the English alphabets a, e, i, o, u.
For Example:
Suppose given character = a
then the output should be “character is the vowel”. Because ‘a’ is a vowel.
Suppose given character = h
then the output should be “character is the consonant”. Because ‘h’ is not a vowel.
Vowel and consonant program in Java
import java.util.*;
public class Main
{
public static void main (String[]args)
{
System.out.println("Vowel and consonant program in Java");
Scanner sc = new Scanner (System.in);
System.out.println ("Please enter a character");
char ch = sc.next ().charAt (0);
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
{
if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i'
|| ch == 'I' || ch == 'o' || ch == 'O' || ch == 'u' || ch == 'U')
System.out.println ("Given Character " + ch + " is a vowel.\n");
else
System.out.println ("Given Character " + ch + " is a consonant.\n");
}
else
System.out.println ("Given Character " + ch +" is neither a vowel nor a consonant.\n");
}
}
Output 1:
Vowel and consonant program in Java
Please enter a character
r
Given Character r is a consonant.
Output 2:
Vowel and consonant program in Java
Please enter a character
e
Given Character e is a vowel.