;

Java Program to Check Whether an Alphabet is Vowel or Consonant


Tutorialsrack 19/04/2021 Java

In this java program, you’ll learn how to check whether an alphabet is a vowel or consonant Java. And we are doing this by using if...else and switch statements in java. The alphabets A, E, I, O, and U (lowercase and uppercase) are known as Vowels, and the rest of the alphabets are known as consonants.

Here are the programs to check whether an alphabet is a vowel or a consonant.

Example1: Java Program to Check Whether an Alphabet is Vowel or Consonant Using if..else statement

Example1: Java Program to Check Whether an Alphabet is Vowel or Consonant Using if..else statement
/* Java Program to Check Whether an Alphabet is Vowel or Consonant Using if..else statement */

import java.util.Scanner;

public class JavaPrograms {

	public static void main(String[] args) {

		Scanner scanner = new Scanner(System.in);
		System.out.println("Enter a character : ");
		char ch = scanner.next().charAt(0);

		// closing scanner class(not mandatory but good practice)
		scanner.close();

		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 if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
			System.out.println(ch + " is consonant");
		} else {
			System.out.println(ch + " is not an alphabet");
		}
	}
}
Output

Enter a character : 

t

t is consonant

 

Enter a character : 

5

5 is not an alphabet

 

Enter a character : 

I

I is vowel

Example2: Java Program to Check Whether an Alphabet is Vowel or Consonant Using the switch statement

Example2: Java Program to Check Whether an Alphabet is Vowel or Consonant Using the switch statement
//Java Program to Check Whether an Alphabet is Vowel or Consonant Using switch statement

import java.util.Scanner;

public class JavaPrograms {

	public static void main(String[] args) {

		boolean isVowel = false;

		Scanner scanner = new Scanner(System.in);
		System.out.println("Enter a character : ");
		char ch = scanner.next().charAt(0);

		// closing scanner class(not mandatory but good practice)
		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(ch + " is not an alphabet");
		}
	}
}
Output

Enter a character : 

t

t is a Consonant

 

Enter a character : 

a

a is  a Vowel

 

Enter a character : 

222

2 is not an alphabet


Related Posts



Comments

Recent Posts
Tags