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. 

Share:

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

In this Java program, we will learn how to find the GCD of two numbers using Java.  GCD (Greatest Common...
  • Java
  • December 3, 2024
In this Java Program, you’ll learn how to swap two numbers using the Java programming language.  How to Swap Two...
  • Java
  • December 2, 2024
In this Java program , we will learn how to Find Largest Element in an Array in your Java program.   How to Find Largest Element...
  • Java
  • December 2, 2024