
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Whether Given Character is Vowel or Consonant in Java
In this article, we will learn to find whether a given character is a vowel or consonant using Java. In the English alphabet, the characters 'a', 'e', 'i', 'o', and 'u' are vowels and the remaining letters are consonants. To find whether the given letter is a vowel or consonant.
Using a loop and or operator verify whether the given character is 'a' or 'e' or 'i' or 'o' or 'u' else it is consonant.
Steps to find whether given character is vowel or consonant
Following are the steps to find whether given character is a vowel or consonant?
- First we will import the Scanner class from java.util package.
- After that prompt the user to enter a character and store it in ch.
- Use an if statement to check if ch is 'a', 'e', 'i', 'o', or 'u'. If true it's a vowel, otherwise, it's a consonant.
- Print whether the character is a vowel or consonant.
Java program to find whether the given character is a vowel or consonant
Below is the Java program to find whether the given character is a vowel or consonant ?
import java.util.Scanner; public class VowelOrConsonant { public static void main(String args[]){ System.out.println("Enter a character :"); Scanner sc = new Scanner(System.in); char ch = sc.next().charAt(0); if(ch == 'a'|| ch == 'e'|| ch == 'i' ||ch == 'o' ||ch == 'u'||ch == ' '){ System.out.println("Given character is an vowel"); }else{ System.out.println("Given character is a consonant"); } } }
Output
Enter a character : a Given character is an vowel Enter a character : l Given character is a consonant
Code Explanation
The program begins by reading a character from the user using the Scanner class. It then checks if the character is one of the vowels ('a', 'e', 'i', 'o', 'u') through an if statement. If the character matches any of these vowels, the program outputs "Given character is a vowel." If it doesn't match, the program concludes that the character is a consonant and prints "Given character is a consonant."