Java Program to Check Whether an Alphabet is Vowel or Consonant

Java Program to Check Whether an Alphabet is Vowel or Consonant

Java if…else Statement

  • The if…else statement in Java is a control flow statement that allows you to execute different code blocks based on the outcome of a condition.
  • You can also use nested if…else statements to handle more complex conditions.

    • You must be familiar with following topics to understand the correspond example Such as: Java if… else statement and Java Switch Statement.

  • To understand the how to Check weather an Alphabet is Vowel or Consonant, Read the Complete Article.

Steps to Check weather an Alphabet is Vowel or Consonant:

  • Here are the steps to Steps to Check weather an Alphabet is Vowel or Consonant:
    • Convert the given alphabet to lowercase, if it is uppercase, as Java considers both upper and lowercase characters as separate characters.
    • Check if the given alphabet is one of the five vowels: ‘a’, ‘e’, ‘i’, ‘o’, ‘u’.
    • If the alphabet is one of the five vowels, print “Vowel”.
    • If the alphabet is not one of the five vowels, print “Consonant”.

The basic Syntax of Java switch Statement:

switch (expression) {
    case value1:
        // code to execute when expression equals value1
        break;
    case value2:
        // code to execute when expression equals value2
        break;
    ...
    default:
        // code to execute when expression doesn't match any of the case values
        break;
}

The basic syntax for if…else statement is:

if (condition) {
    // statements to execute if the condition is true
} else {
    // statements to execute if the condition is false
}

Let’s look at the Java Program to Check weather an Alphabet is Vowel or Consonant to perform certain operations.

Example 1: Java Program to Check weather an Alphabet is Vowel or Consonant

Run
public class Main {
    public static void main(String[] args) {
        char ch = 'b';

        if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' )
            System.out.println(ch + " is a vowel.");
        else
            System.out.println(ch + " is a consonant.");
    }
}

Output

 b is a consonant.

Example 2 :Java Program to Check weather an Alphabet is Vowel or Consonant

Run
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter an alphabet: ");
        char ch = sc.next().charAt(0);
        ch = Character.toLowerCase(ch);

        switch(ch) {
            case 'a':
            case 'e':
            case 'i':
            case 'o':
            case 'u':
                System.out.println(ch + " is a vowel.");
                break;
            default:
                System.out.println(ch + " is a consonant.");
        }
    }
}

Output

Enter an alphabet: A
a is a vowel.

Example 3: Java Program to Check weather an Alphabet is Vowel or Consonant.

Run
public class Main{

    public static void main(String[] args) {

        char ch = 'i';

        if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' )
            System.out.println(ch + " is vowel");
        else
            System.out.println(ch + " is consonant");

    }
}

Output

i is vowel

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription