Java Program to Check if a Character is a Vowel or Consonant

In this Java program, you’ll learn how to check if a character is a vowel or consonant using Java with some sample code snippets

How to Check if a Character is a Vowel or Consonant? 

Example 1: 

RUN CODE SNIPPET
import java.util.Scanner; 

public class Main { 

   public static void main(String args[]) 

{ 

      System.out.println("Enter a character :"); 

      Scanner sc = new Scanner(System.in); 

      char ch = sc.next().charAt(0); 

      if(ch == 'a'|| ch == 'e'|| ch == 'i' ||ch == 'o' ||ch == 'u'||ch == ' ') 

{ 

         System.out.println("The given character is a vowel"); 

      } 

Else 

{ 

         System.out.println("The given character is a consonant"); 

      } 

   } 

}

OUTPUT 

Enter a character: e 

Given character is a vowel

The alphabets ‘a’, ‘e’, ‘i’, ‘o’, ‘u’ are vowels and the remaining are called consonants. 

 In the above example, we are using a loop and or operator to find whether the given character is vowels are consonants. 

Example 2: 

RUN CODE SNIPPET
public class Main { 

    public static void main(String[] args) { 

        char ch = 'r'; 

        switch (ch) { 

            case 'a': 

            case 'e': 

            case 'i': 

            case 'o': 

            case 'u': 

                System.out.println(ch + " is vowel"); 

                break; 

            default: 

                System.out.println(ch + " is consonant"); 

        } 

    } 

}

OUTPUT 

r is consonant

In the above example, we are using the switch to find whether the given character is vowels are consonants.