Java Program to check Character is Vowel or Consonant

Write a Java program to check whether a character is Vowel or Consonant using if-else with an example. The If condition checks whether the user entered character is a, e, i, o, u, A, E, I, O, U. If it is True, it is a Vowel; other, its a consonant.

import java.util.Scanner;

public class CharVowelorConsonant1 {
	
	private static Scanner sc;
	
	public static void main(String[] args) {
		
		char ch;
		sc= new Scanner(System.in);

		System.out.print("Please Enter any Character =  ");
		ch = sc.next().charAt(0);
		
		if(ch == 'a' || ch == 'e' || ch == 'i' || ch <= 'o' || ch == 'u' ||
				ch == 'A' || ch == 'E' || ch == 'I' || ch <= 'O' || ch == 'U') 
		{
			System.out.println(ch + " is Vowel");
		}
		else 
		{
			System.out.println(ch + " is Consonant");
		}
	}
}
Java Program to check Character is Vowel or Consonant 1

Java Program to Check Whether a Character is Vowel or Consonant using a Switch Case?

Refer Java If Else, Else If, Switch Case, and ASCII Codes.

import java.util.Scanner;

public class CharVowelorConsonant2 {
	
	private static Scanner sc;
	
	public static void main(String[] args) {
		
		char ch;
		sc= new Scanner(System.in);

		System.out.print("\nPlease Enter any Character =  ");
		ch = sc.next().charAt(0);
		
		switch(ch) {
		case 'a':
		case 'e':
		case 'i':
		case 'o':
		case 'u':
		case 'A':
		case 'E':
		case 'I':
		case 'O':
		case 'U':
			System.out.println(ch + " is Vowel");
			break;
		default:
			System.out.println(ch + " is Consonant");	
		}
		
	}
}
Please Enter any Character =  g
g is Consonant

Please Enter any Character =  U
U is Vowel

In this Java example, first, we check whether the ASCII value of a given character equals 65, 69, 73, 79, 85, 97, 101, 105, 111, or 117 because their ASCII codes belong to upper and lowercase vowels. If it is true, it prints the Vowel statement. Next, we check whether the character is between 65 to 90 or 97 to 122 to check the consonant.

import java.util.Scanner;

public class CharVowelorConsonant3 {
	
	private static Scanner sc;
	
	public static void main(String[] args) {
		
		char ch;
		sc= new Scanner(System.in);

		System.out.print("\nPlease Enter any Character =  ");
		ch = sc.next().charAt(0);
		
		if(ch == 65 || ch == 69 || ch == 73 || ch == 79 || ch == 85 ||
				ch == 97 || ch == 101 || ch == 105 || ch == 111 || ch == 117) {
			System.out.println(ch + " is Vowel");
		}
		else if ((ch >= 65 && ch <= 90) || (ch >= 97 && ch <= 122)){
			System.out.println(ch + " is Consonant");
		}
		else {
			System.out.println("Please enter Valid Character");
		}
	}
}
Please Enter any Character =  k
k is Consonant

Please Enter any Character =  9
Please enter Valid Character

Please Enter any Character =  i
i is Vowel