Java program to check whether an alphabet is vowel or consonant
Sure, here's a Java program that checks whether an alphabet is a vowel or consonant:
import java.util.Scanner; public class VowelOrConsonant { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter an alphabet: "); char ch = sc.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 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"); } } }Sourwww:ec.theitroad.com
In this program, we first use a Scanner
to obtain an alphabet from the user. We then check if the alphabet is a vowel or a consonant by comparing it to the vowels 'a', 'e', 'i', 'o', 'u' and their uppercase counterparts, and checking if it falls outside of this range but is still a letter.
If the alphabet is a vowel, we print a message saying so. If it is a consonant, we print a message saying so. If it is not a letter, we print a message saying that it is not an alphabet. Note that we use the charAt
method to extract the first character from the input string, since we only want to check a single alphabet.