Java Program to Print character is vowel or Consonant

In this post, we will write a two java program to check if the character is vowel or Consonant using the If-else statement and Switch case.

How the program will work?

  • If character match to (a,e,i,o,u) than its Vowel
  • Else, it is Consonant

Example:

  1. Input:
  2. Enter the Character: E
  3. Output:
  4. E is Vowel

Example 1: Program to check character is Vowel or Consonant using if-else

This program asks the user to Enter the Character and based on the user input value, the program checks if the enter character match (a, e, i, o, u) than its Vowel else it is Consonant. To accomplish this goal, we are using an If-else statement.

import java.util.Scanner;
public class vowel_or_consonant {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter the Character: ");
        char chr = sc.next().toLowerCase().charAt(0);

        if(chr=='a'|| chr=='e'|| chr=='i' ||chr=='o' || chr=='u'){
            System.out.println(chr+" is Vowel");
        }
        else {
            System.out.println(chr+" is Consonant");
        }

    }
}

Output:

Enter the Character: e
e is Vowel

Example 2: Program to check character is Vowel or Consonant using switch-case

In this program, we will deal with multiple conditions. In this Java Program, we are using switch case to return the its Vowel or Consonant.

import java.util.Scanner;
public class vowel_or_consonant {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter the Character: ");
        char chr = sc.next().toLowerCase().charAt(0);

        switch (chr)
        {
            case 'a':
            case 'e':
            case 'i':
            case 'o':
            case 'u':
                System.out.println(chr+" is Vowel");
                break;
            default:
                System.out.println(chr+" is Consonant");
        }
    }
}

Output:

Enter the Character: r
e is Consonant

Recommended:

  1. Java Program to solve quadratic equations
  2. Java Program to find the greatest of three Numbers
  3. Java Program to Display the weekday between 1 and 7
  4. Java Program to Find the number of days in a Months.
  5. Java Program Count invalid Mobile numbers form list
  6. Java program Fill a bucket with water using a mug
  7. Java Program to Check if Number is Positive or Negative
  8.  

Leave a Comment

Your email address will not be published. Required fields are marked *

x