Vowel and Consonant program in Java
In the given Java program, you will learn how to check whether the given character is a vowel or consonant.
The alphabet is made up of 26 letters, 5 of which are vowels (a, e, i, o, u) and the rest of which are consonants. There are different ways in Java programming to check whether an alphabet is a vowel or consonant. Here, we have mentioned four of them.
Java program to check vowel or consonant using If Else statement
In the below implementation, we check if the entered character corresponds to any of the five vowels. And if it matches, "Vowel" is printed, else "Consonant" is printed.
// Java program for counting vowels
import java.util.Scanner;
public class CheckVowelConsonant {
public static void main(String[] args) {
System.out.print("Please enter a character: ");
Scanner sc = new Scanner(System.in);
char chr = sc.next().charAt(0);
if(chr == 'a' || chr == 'e' || chr == 'i'
|| chr == 'o' || chr == 'u' )
System.out.println(chr + " is vowel.");
else
System.out.println(chr + " is consonant.");
}
}
Output of the above code:
Please enter a character: p
p is consonant.
Please enter a character: u
u is vowel.
Java program to check vowel or consonant using Switch statement
In the given Java program, instead of using an if condition, we replace it with a switch case statement.
// Java program for counting vowels
import java.util.Scanner;
class CheckVowelConsonant
{
public static void main(String[ ] arg)
{
int i=0;
Scanner sc=new Scanner(System.in);
System.out.print("Enter a character: ");
char chr=sc.next( ).charAt(0);
switch(chr)
{
case 'a' :
case 'e' :
case 'i' :
case 'o' :
case 'u' :
case 'A' :
case 'E' :
case 'I' :
case 'O' :
case 'U' :
i++;
}
if(i==1)
System.out.println(chr+" is vowel.");
else
if((chr>='a'&&chr<='z')||(chr>='A'&&chr<='Z'))
System.out.println(chr+" is consonent.");
else
System.out.println("Not an alphabet.");
}
}
Output of the above code:
Enter a character: h
h is consonent.
Enter a character: i
i is vowel.
Enter a character: M
M is consonent.
Java program to check vowel or consonant using indexOf() method
The indexOf() method returns the index within this string of the first occurrence of the specified substring, starting at the specified index. Here, we have used this method to check whether the entered character is in the list of vowels.
// java program to check whether input
// character is a vowel or consonant
import java.io.*;
import java.util.Scanner;
class CheckVowelConsonant {
// Function to find whether an input
// character is vowel or not
static String chkVowel(char ch)
{
// Make the list of vowels
String str = "aeiouAEIOU";
return (str.indexOf(ch) != -1) ? "vowel"
: "consonant";
}
// Driver Code
public static void main(String[] args)
{
System.out.print("Please enter a character: ");
Scanner sc = new Scanner(System.in);
char chr = sc.next().charAt(0);
System.out.println(chr+" is a "+chkVowel(chr));
}
}
Output of the above code:
Please enter a character: m
m is a consonant
Please enter a character: p
p is a consonant
Please enter a character: u
u is a vowel
Java program to check vowel or consonant using user-defined function
In the given Java program, we have defined a function to check whether the given character is vowel or consonant.
import java.util.Scanner;
class Char
{
void CheckVowelConsonant(char ch)
{
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
{
System.out.println("Entered character "+ch+" is Vowel");
}
else if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
System.out.println("Entered character "+ch+" is Consonent");
else
System.out.println("Not an alphabet");
}
public static void main(String[ ] arg)
{
Char ch=new Char();
Scanner sc=new Scanner(System.in);
System.out.println("Please enter a character: ");
char in=sc.next( ).charAt(0);
ch.CheckVowelConsonant(in);
}
}
Output of the above code:
Please enter a character: h
Entered character h is Consonent
Please enter a character: i
Entered character i is Vowel
Related Articles
Sort array in ascending order JavaAutomorphic number in Java
Pascal triangle program in Java
Factorial using recursion in java
Java random number between 1 and 10
Palindrome program in Java
Floyd triangle in Java
Pyramid pattern programs in Java
Star pattern programs in Java
Number pattern programs in Java
Java program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java