Java program to check a character is a vowel or consonant

A character is a vowel or consonant using java

In English, we generally have five vowels which are A , E , I , O , U in uppercase and a , e , i , o , u in lowercase out of 26 alphabets.Consonants are the alphabets except vowels(a, e ,i ,o ,u). We can say consonants are opposite to the vowels.

In this article,  we will explain you the working and the code of the program for checking the particular character entered by the user is Vowel or Consonant. The program is written in the Java language with appropriate output.

Java program to check a character is a vowel or consonant

Algorithm:-

Step 1 : Ask the user to input a character.

Step 2 : Check whether character is a vowel or not. For checking vowel, we need to compare the user input character with all five vowels in uppercase as well as five vowels in lowercase. This is our first condition in if statement.

Step 3 : Our second condition will be for Consonants, i.e. we will check the character entered by the user is Consonant or not.Condition will be  (character >= ‘A’  or character <= ‘Z’ ) and (character >= ‘a’ or character <= ‘z’). This will be in else-if statement.

Step 4 : In this step we have to create a else statement for printing that the character entered by the user is not an alphabet because if a character is neither a vowel nor a consonant then surely the character is not an alphabet.

Step 5 : Print the statement or can say, display the result.

Java Code:-

//JAVA Program to check whether the character entered by user is Vowel or Consonant.

import java.util.Scanner;
public class vowelorconsonant
{
              //class declaration
                public static void main(String[] args)
{
                  //main method declaration
                              Scanner sc=new Scanner(System.in);         //scanner class object creation

                              System.out.println(" Enter a character");
                              char c = sc.next().charAt(0);         //taking a character c as input from user

                              if(c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U'

                              || c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')             //condition for the vowels

                                             System.out.println(" Vowel");

                              else if((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))               //condition for the consonants

                                             System.out.println(" Consonant");
                              else
                                             System.out.println(" Not an Alphabet");

                              sc.close()       //closing scanner class(not mandatory but good practice)
               }               //end of main method
}               //end of class
Output:
Enter a character
U
vowel
Enter the character
w
consonant
Enter the character
&
not an aphabet