Java Program to check vowel or consonant using if-else(ASCII)

In the following question, we are supposed to ask the user to enter a character and check if it is a vowel or consonant using if-else construct.

The standard algorithm will be:

  1. Enter and Store the User Input.
  2. Create a Duplicate of the user input and change its case for uniformity.
  3. Use if-else construct to compare. Now there might be to sub-cases: Either it might be a vowel or might not be.
  4. If it is a vowel, straightforwardly print a result of success.
  5. If it is not a vowel, it either might be a consonant or a special characters.
  6. Use ASCII Values of Alphabets to do the same.
  7. Print result accordingly.
ASCII VALUES:

A-Z: 65 to 90. a-z: 97 to 122. 0-9: 48 to 57.

Source Code:

/* Program to check if entered character is vowel or consonant using if-else*/

import java.util.*; class ConsVowel { public static void main() { Scanner inp=new Scanner(System.in); System.out.print("\n Enter Character: "); char c=((inp.nextLine()).charAt(0)); char z=Character.toUpperCase(c); //Changing Value to Upper Case for uniformity.

if(z=='A' || z=='E' || z=='I' || z=='O' || z=='U') //Checking if Vowel System.out.println(c+" is a Vowel."); else { if((c>=65 && c<=90) || (c>=97 && c<=122) ) // Checking if character is special character. System.out.println(c+" is a Consonant."); else System.out.println(c+" is a Special Character."); }

} }

Output:

Enter Character: X
X is a Consonant.

Enter Character: i i is a Vowel.

Enter Character: @ @ is a Special Character.

Java Program to check vowel or consonant using if-else(ASCII)