Java program to check given character is Vowel or Consonant

Java program to check given character is Vowel or Consonant

In this tutorial, you will learn to write Java Program to check that given characters or alphabets are vowels or consonants.

We will be using the if else statement of Java to write our program.

For Example:

Suppose the Given Character input is ‘a’

The output should be “Given character is the vowel”

And Suppose the Given Character is ‘b’

Then the Output should be “Given character is consonant”.

Java Program to check given alphabet is Vowel or Consonant (If -else)

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.");
	        else
	            System.out.println("Given Character " + ch + " is a consonant.");
        }
        else
            System.out.println("Given Character " + ch +" is neither a vowel nor a consonant.");
	    sc.close();
	}
}

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.

Java Program to check given alphabet is Vowel or Consonant (Switch Statement)

import java.util.*;
public class Main
{
	public static void main(String args[])
	{
		System.out.println("Vowel and consonant program in Java using Switch Case");
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter a character");
        char ch = sc.next().charAt(0);
        switch (ch) {
            case 'a':
            case 'e':
            case 'i':
            case 'o':
            case 'u':
                System.out.println ("Given Character " + ch + " is a vowel.");
                break;
            default:
                System.out.println("Given Character " + ch + " is a consonant.");
        }
	    sc.close();
	}
}

Output 1

Vowel and consonant program in Java using Switch Case
Please enter a character
t
Given Character t is a consonant.

Output 2

Vowel and consonant program in Java using Switch Case
Please enter a character
i
Given Character i is a vowel.