Java Program to Check Character is Vowel or Consonant

In the previous article we have discussed about Java Program to Print ASCII Value of All Character

In this article we are going to see how to check the input alphabet is vowel or consonant by using Java programming language.

Java Program to Check Character is Vowel or Consonant

There are 26 alphabet and in those 26, five are vowel and rest of the alphabets are consonant. Vowels are a,e,i,o,u. So, we are going to check in this program that the input alphabet is containing any vowel value or not if yes then print it is vowel or it is consonant.

Let’s see different ways to check if the character is vowel or consonant.

Method-1: Java Program to Check Character is Vowel or Consonant By Using If-else Statement

Approach:

  • Declare a character variable as ch.
  • Prompt the user to Enter a character as input value of ch.
  • First, we are going to check whether the input character is an alphabet or not by performing a condition check (IF-ELSE).
  • If the input character is an alphabet, then we are going to perform an another condition check where the input alphabet is going to compare with each of five vowels then if the condition is satisfied then print the current input character is a vowel otherwise it is a consonant.

Program:

import java.util.Scanner;
public class Main
{
    public static void main(String[] args)
    {
        //create an object of the scanner class
        Scanner inp= new Scanner(System.in);
        System.out.print("Enter the Character: ");
        //take the input from user
        char ch=inp.next().charAt(0);
        //check the input character is alphabet or not
        if((ch>=65 && ch<=90) || (ch>=97 && ch<=122))
        {
            //check the input alphabet is vowel or not
            if(ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U' || ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u')
            {
                System.out.println(ch+" is a vowel.");
            }
            else
            {
                System.out.println(ch+" is a Consonant.");
            }
        }
        else
        {
            System.out.println(ch+" is not an Alphabet.");
        }
    }
}
Output:

Enter the Character: f
f is a Consonant.

Method-2: Java Program to Check Character is Vowel or Consonant By Using Switch Case

Approach:

  • Declare a character variable as c.
  • Prompt the user to enter an alphabet as character input.
  • Create a switch case and take c as condition.
  • Inside switch case take all the vowels as cases and leave it blank, so that the condition will be check in each case.
  • If the input is vowel, the case satisfied and it will print the alphabet is vowel and when any case satisfied then break statement will execute and it will stop checking and going out from the switch case.
  • If any of the case is not satisfied then it will print the default case that is the alphabet is consonant.

Program:

import java.util.Scanner;
public class Main
{
    public static void main(String[] args) 
    {
        System.out.print("Enter an Alphabet: ");
        Scanner s=new Scanner(System.in);
        char c = s.next().charAt(0);;
        switch (c) 
        {
            case 'a':
            case 'e':
            case 'i':
            case 'o':
            case 'u':
            case 'A':
            case 'E':
            case 'I':
            case 'O':
            case 'U':
                System.out.println(c + " is a vowel.");
                break;
            default:
                System.out.println(c + " is a consonant.");
        }
    }
}
Output:

Enter an Alphabet: E
E is a vowel.

The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.

Related Java Programs: