Java Program to Check a Character is Vowel or Consonant

  • Java Program to check whether an alphabet is vowel or not using if else statement.
  • Write a java program to check a character is vowel or consonant using switch case statement.

Given a character we have to check whether it is vowel or consonant character.

A Vowel alphabets represents a speech sound created by the relatively free passage of breath through the larynx and oral cavity. Alphabets that are not vowels are Consonant. English has five proper vowel letters (A, E, I, O, U) all remaining alphabets are consonants.

To check whether a character is vowel or not we have to compare it with both lowercase and uppercase vowel characters (A, E, I, O, U, a, e, i, o, u).

Java Program to check Vowel or Consonant

In the java program, we take a character input from user and store in variable "c". Then, we check whether it is any one of these ten characters(lower and upper case vowels) a, A, e, E, i, I, o, O, u and U using || operator. If c matches with any vowel character, then it is a vowel otherwise a consonant.

package com.tcc.java.programs;

import java.util.Scanner;

/**
 * Java Program to check a character is vowel or consonant
 */
public class CheckVowel {
    public static void main(String[] args) {
        char c;
        Scanner scanner;
        scanner = new Scanner(System.in);

        System.out.println("Enter an Alphabet");
        c = scanner.next().charAt(0);

        /* Check if input alphabet is member of set{A,E,I,O,U,a,e,i,o,u} */
        if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' || c == 'E' || c == 'I' || c == 'O'
                || c == 'U') {
            System.out.println(c + " is Vowel");
        } else {
            System.out.println(c + " is Consonant");
        }
    }
}
Output
Enter an Alphabet
A
A is Vowel
Enter an Alphabet
d
d is Consonant

Java Program to check character is vowel or consonant using Switch case statement

This java program is similar to above program except here we are using switch case statement to compare input characters with vowel characters.

package com.tcc.java.programs;

import java.util.Scanner;

/**
 * Java Program to check a character is vowel or consonant using switch case
 * statement
 */
public class CheckVowelSwitch {
    public static void main(String[] args) {
        char c;
        Scanner scanner;
        scanner = new Scanner(System.in);

        System.out.println("Enter an Alphabet");
        c = scanner.next().charAt(0);

        if (isVowel(c)) {
            System.out.println(c + " is Vowel");
        } else {
            System.out.println(c + " is Consonant");
        }
    }

    /**
     * Returns true if c is vowel alphabet otherwise false.
     */
    public static boolean isVowel(char c) {
        /*
         * Check if input alphabet is member of set{A,E,I,O,U,a,e,i,o,u} using
         * switch statement
         */
        switch (c) {
        case 'A':
        case 'E':
        case 'I':
        case 'O':
        case 'U':
        case 'a':
        case 'e':
        case 'i':
        case 'o':
        case 'u':
            return true;
        default:
            return false;
        }
    }
}
Output
Enter an Alphabet
e
e is Vowel
Enter an Alphabet
Z
Z is Consonant