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

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

You May Also Like

Java is a popular programming language that is used to develop a wide range of applications. If you are a...
  • Java
  • March 6, 2023
Java is a programming language and computing platform that is used to create various types of applications. It is used...
  • Java
  • March 6, 2023
In this post, you’ll learn how to download and install JUnit in Eclipse so that you can use in your...
  • Java
  • October 9, 2022