Java Program to Check Whether an Alphabet is Vowel or Consonant

JavaObject Oriented ProgrammingProgramming

In this article, we will understand how to check whether an alphabet is vowel or consonant. Alphabets that include 'a' 'e' 'i' 'o' 'u' are called Vowels and all other alphabets are called Consonants.

Below is a demonstration of the same −

Input

Suppose our input is −

Enter the character : i

Output

The desired output would be −

The character : i is a vowel

Algorithm

Step 1 - START
Step 2 - Declare a char value namely my_input.
Step 3 - Read the required values from the user/ define the values
Step 4 - Using an if condition, check if the input is equal to 'a' 'e' 'I' 'o' 'u' values. If yes, its an vowel. Else it's a consonant.
Step 5 - Display the result
Step 6 - Stop

Example 1

Here, the input is being entered by the user based on a prompt. You can try this example live in our coding ground tool run button.

import java.util.Scanner;
public class VowelAndConsonant {
   public static void main(String[] args) {
      char my_input;
      System.out.println("Required packages have been imported");
      Scanner my_scanner = new Scanner(System.in);
      System.out.println("A reader object has been defined ");
      System.out.print("Enter the character : ");
      my_input = my_scanner.next().charAt(0);
      if(my_input == 'a' || my_input == 'e' || my_input == 'i' || my_input == 'o' || my_input == 'u' )
         System.out.println("The character : " +my_input + " is a vowel");
      else
         System.out.println("The character : " +my_input + " is a consonant");
   }
}

Output

Required packages have been imported
A reader object has been defined
Enter the character : i
The character : i is a vowel

Example 2

Here, the integer has been previously defined, and its value is accessed and displayed on the console.

public class VowelAndConsonant {
   public static void main(String[] args) {
      char my_input = 'i';
      System.out.println("The character is defined as :" +my_input);
      if(my_input == 'a' || my_input == 'e' || my_input == 'i' || my_input == 'o' || my_input == 'u' )
         System.out.println("The character : " +my_input + " is a vowel");
      else
         System.out.println("The character : " +my_input + " is a consonant");
   }
}

Output

The character is defined as :i
The character : i is a vowel
raja
Updated on 22-Feb-2022 12:12:20

Advertisements