Table of Contents
Introduction
This Java program checks if the character in question is a vowel or a consonant. The vowels in the English alphabet are a, e, i, o, and u. The consonants in the English alphabet are b, c, d, f, ….., that is, all letters that are not vowels.
Examples:
Input: char = r
Output: Consonant
Input: char = e
Output: Vowel
Approach:
We will take a character as input or otherwise and then check whether the character equals either a, or e, or i, or o, or u. If it does, we will print out that the character is a vowel. If it does not, we will print out that character is a consonant.
Java Program to Check Whether the Character is Vowel or Consonant
Sample Code 1
//This program will check if the character input is either a vowel or a character import java.io.*; public class Demo { //Required function given below static void vowelCheck (char y) { if (y == ‘a’ || y == ‘e’ || y == ‘i’ || y = ‘o’ || y == ‘u’) System.out.println (“It is a vowel”); else System.out.println (“It is a consonant”); } //Main Method static public void main (String args []) { VowelCheck (‘b’); VowelChecl (‘u’); } }
OUTPUT
It is a consonant
It is a Vowel
Sample Code 2
This code also checks for capital letter characters.
//This program will check if the character input is either a vowel or character, including capital //letters import java.io.*; public class Demo { //Required function given below static void vowelCheck (char y) { if (y == ‘a’ || y == ‘e’ || y == ‘i’ || y = ‘o’ || y == ‘u’ || y == ‘A’ || y == ‘E’ || y == ‘I’ || y = ‘)’ || y == ‘U’) System.out.println (“It is a vowel”); else System.out.println (“It is a consonant”); } //Main function static public void main (String args []) { VowelCheck (‘W’); VowelChecl (‘I’); } }
OUTPUT
It is a consonant
It is a Vowel
Sample Code 3
The program below will use the indexOf method of characters to implement the program.
//This program will check if the character input is either a vowel or character using the indexOf //method () import java.io.*; public class Demo { //Required function given below static void vowelCheck (char y) { //Noting down all vowels, both small and capital letters String str = “aeiouAEIOU”; return (str.indexOf(y) ! = -1) ? “Vowel” : “Consonant” ; } //Main Method public static void main (String args []) { System.out.println (“It is a ” + vowelCheck (‘a’)); System.out.println (“It is a ” + vowelCheck (‘x’)); } }
OUTPUT
It is a Vowel
It is a Consonant
0 Comments