Skip to main content

ProwessApps

Learn · Practice · Excel

Java Program to check the alphabet is vowel or not.

Java Code Example — Conditional Programs

ADVERTISEMENT

Java Program to check the alphabet is vowel or not.

Objective

Write a Java program to check if an entered character is a vowel or a consonant.

Algorithm / Approach

  1. Prompt the user to input a single character.
  2. Read the character using System.in.read().
  3. Check if the character matches any lowercase vowel: a, e, i, o, u.
  4. Use an else if to check if it matches any uppercase vowel: A, E, I, O, U.
  5. If it doesn't match any of them, it is not a vowel.
Test.java
import java.util.Scanner;
class Test {
 public static void main(String[] a) throws Exception
 {
  Scanner s=new Scanner(System.in);
  System.out.print("Enter a Character: ");
  char x =(char)System.in.read();
  if(x=='a' ||x=='e' ||x=='i' ||x=='o' ||x=='u') {
   System.out.print("Character is vowel");
  }
  else if(x=='A' ||x=='E' ||x=='I'||x=='O' ||x=='U') {
   System.out.print("Character is vowel");
  }
  else {
   System.out.print("Character is NOT vowel");
  }
 }
}

Expected Output

Enter a Character: A
Character is vowel

Explanation of the Program

  • This program uses the logical OR operator || to check multiple possible equality conditions at once.
  • If the character matches just one of the conditions (e.g., it is 'e'), the entire condition becomes true.
  • It reads a raw byte from the console and casts it to a char.
  • Because System.in.read() can throw an IOException, the main method signature includes throws Exception.

Complexity

Time Complexity O(1)
Space Complexity O(1)

Common Mistakes

  • Using double quotes "a" instead of single quotes 'a' for characters.
  • Forgetting to check for uppercase vowels, causing "A" to be incorrectly marked as a consonant.
ADVERTISEMENT