Java Program to Check Whether an Alphabet is Vowel or Consonant

In this program, you'll learn to check whether an alphabet is a vowel or a consonant using if..else and switch statement in Java.

Check whether an alphabet is a vowel or consonant using if..else statement

package com.javaguides.java.tutorial; /**  * Java Program to Check Whether an Alphabet is Vowel or Consonant  *   * @author https://www.sourcecodeexamples.net/  *  */ public class JavaProgram { public static void main(String[] args) { char character = 'a'; if (character == 'a' || character == 'e' || character == 'i' || character == 'o' || character == 'u') { System.out.println(character + " is vowel"); } else { System.out.println(character + " is consonant"); } } }
Output:
a is vowel

Check whether an alphabet is a vowel or consonant using a switch statement

package com.javaguides.java.tutorial; import java.util.Scanner; /**  * Java Program to check Vowel or Consonant using Switch Case  *   * @author https://www.sourcecodeexamples.net/  *  */ public class JavaProgram { public static void main(String[] args) { try (Scanner scanner = new Scanner(System.in)) { boolean isVowel = false; System.out.println("Enter a character : "); char ch = scanner.next().charAt(0); scanner.close(); switch (ch) { case 'a': case 'e': case 'i': case 'o': case 'u': case 'A': case 'E': case 'I': case 'O': case 'U': isVowel = true; } if (isVowel == true) { System.out.println(ch + " is a Vowel"); } else { if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) { System.out.println(ch + " is a Consonant"); } else { System.out.println("Input is not an alphabet"); } } } } }
Output:
Enter a character : J J is a Consonant

Related Java Programs


Comments