Java Program to Check Character is Vowel or Consonant

In Java, we can check if a character is a vowel or consonant using if-else conditions or the ternary operator. In this article, we focused on learning through examples, algorithms, detailed logic, and program explanation for better understanding.

Required Knowledge

Problem Statement

We need to write a java program to check given character is vowel or consonant. The Program should take a character as input and It should output the result whether it is a Vowel or consonant. This above action should be done in our program. Let’s look at an example,

Example 1:

Input: A
Output: A is Vowel

Here, for the input A, Output is “A is Vowel” as A is an Vowel

Example 2:

Input: M
Output: M is Consonant

Here for the input M, Output is ” M is Consonant” as M is not an vowel

Algorithm to Check character is Vowel or Consonant

  • Start: Begin the program with character input ‘ch’.
  • Use the next() method to read and extract the first character using charAt(0).
  • Condition Check:
    • If the character is ‘A’, ‘E’, ‘I’, ‘O’, ‘U’ (uppercase) or ‘a’, ‘e’, ‘i’, ‘o’, ‘u’ (lowercase), we identify it as a vowel.
    • if ((ch == ‘A’ || ch == ‘E’ || ch == ‘I’ || ch == ‘O’ || ch == ‘U’) ||
    • (ch == ‘a’ || ch == ‘e’ || ch == ‘i’ || ch == ‘o’ || ch == ‘u’)), Otherwise, it is a consonant.

Logic to check if an character is Vowel or Consonant

There are multiple logics to check a given input character is vowel or consonant in Java Programing language.

Logic 1: Using if-else

  • With an if-else statement, if the input character belongs to ‘A’, ‘E’, ‘I’, ‘O’, ‘U’, ‘a’, ‘e’, ‘i’, ‘o’, ‘u’, it’s considered a vowel. Otherwise, it’s treated as a consonant
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");
	}

Logic 2: Ternary Operator

  • This operator works like if-else statements: if the condition is true, it evaluates the first expression; if false, it evaluates the second.
  • To check for vowels in Java, if the input character is ‘A’, ‘E’, ‘I’, ‘O’, ‘U’, ‘a’, ‘e’, ‘i’, ‘o’, ‘u’, it’s considered a vowel. Otherwise, it’s treated as a consonant. We will store the result of this check in a variable named ‘result’.
String result;
		result =((ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')||
		(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')) ?
			ch+" is vowel": ch+" is consonant";
		System.out.println(result);

Java Program to Check Character is Vowel or Consonant

Program 1: Using if-else

In the below program we are using if else to check given character is vowel or consonant in Java.

import java.util.Scanner;

public class VoewlConsonant {

	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		System.out.println("Enter the character");
		char ch = s.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");
		}
		s.close();
	}
}

Output:

Enter the character
I
I is vowel

Java program explanation to check given character is Vowel or Consonant

  • The program imports the Scanner class from the java.util package to facilitate user input.
  • Program defines a class named VoewlConsonant.
  • The program contains the main method where the program execution starts.
  • Asks the user to enter a character.
  • Reads the user input character using the Scanner object’s next() method and extracts the first character using charAt(0).
  • It checks if the input character is a vowel by comparing it against a set of predefined vowels (both uppercase and lowercase).
  • If the input character matches any of the vowels, it prints that the character is a vowel.
  • Otherwise, it prints that the character is a consonant.
  • It closes the Scanner object s to release system resources.

Program 2: Using Ternary Operator

In the below program we are using Ternary Operator to check given character is vowel or consonant in Java.

import java.util.Scanner;

public class VoewlConsonant {

	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		System.out.println("Enter the character");
		char ch = s.next().charAt(0);
		String result;
		result =((ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')||
				(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')) ?
			ch+" is vowel": ch+" is consonant";
		System.out.println(result);
		s.close();
	}
}

Output:

Enter the character
c
c is consonant

Java program explanation to check given character is Vowel or Consonant

  • The program imports the Scanner class from the java.util package to facilitate user input.
  • Program defines a class named VoewlConsonant.
  • The program contains the main method where the program execution starts.
  • Asks the user to enter a character.
  • Reads the user input character using the Scanner object’s next() method and extracts the first character using charAt(0).
  • It uses a ternary operator to check if the input character is a vowel or a consonant.
  • Then prints the value of the result variable.
  • If the input character matches any of the vowels, it assigns a message indicating that the character is a vowel to the result variable.
  • Otherwise, it assigns a message indicating that the character is a consonant to the result variable.
  • It closes the Scanner object s to release system resources.

Conclusion

In this article, we explored how to check if a character is vowel or consonant using if-else statements and the ternary operator. We understood the logic behind this and moved on to writing the program.

For more information on related topics, you might find following articles helpful:

  • Understanding the concepts of Conditional statements in Java
  • Concepts of operators in Java
  • Top 10 Java Programming tips

We hope you find this article helpful. If you have any doubts or need any further clarification, feel free to comment below. Don’t forget to subscribe to our newsletter to get more updates on Java Programming tips and tutorials.

Happy Java Learning!

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.