Java Program to Check Vowel or Consonant
if statements to compare the character against vowels like 'a', 'e', 'i', 'o', 'u' (case-insensitive), and print "Vowel" if it matches or "Consonant" otherwise.Examples
How to Think About It
Algorithm
Code
import java.util.Scanner; public class VowelOrConsonant { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a character: "); char ch = scanner.next().charAt(0); if (!Character.isLetter(ch)) { System.out.println(ch + " is not an alphabet"); } else { char lowerCh = Character.toLowerCase(ch); if (lowerCh == 'a' || lowerCh == 'e' || lowerCh == 'i' || lowerCh == 'o' || lowerCh == 'u') { System.out.println(ch + " is a vowel"); } else { System.out.println(ch + " is a consonant"); } } scanner.close(); } }
Dry Run
Let's trace the input 'B' through the code
Input character
User inputs 'B'
Check if letter
'B' is a letter, so continue
Convert to lowercase
'B' becomes 'b'
Check vowel
'b' is not 'a', 'e', 'i', 'o', or 'u'
Print result
Print 'B is a consonant'
| Step | Character | Is Letter? | Lowercase | Is Vowel? | Output |
|---|---|---|---|---|---|
| 1 | B | Yes | b | No | B is a consonant |
Why This Works
Step 1: Check if input is a letter
We use Character.isLetter() to ensure the input is an alphabet letter before checking vowels or consonants.
Step 2: Convert to lowercase
Converting to lowercase with Character.toLowerCase() simplifies vowel checking by handling uppercase and lowercase uniformly.
Step 3: Compare with vowels
We compare the character with vowels using if conditions; if it matches, we print vowel, else consonant.
Alternative Approaches
import java.util.Scanner; public class VowelOrConsonantSwitch { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a character: "); char ch = scanner.next().charAt(0); if (!Character.isLetter(ch)) { System.out.println(ch + " is not an alphabet"); } else { switch (Character.toLowerCase(ch)) { case 'a': case 'e': case 'i': case 'o': case 'u': System.out.println(ch + " is a vowel"); break; default: System.out.println(ch + " is a consonant"); } } scanner.close(); } }
import java.util.Scanner; public class VowelOrConsonantString { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a character: "); char ch = scanner.next().charAt(0); if (!Character.isLetter(ch)) { System.out.println(ch + " is not an alphabet"); } else { String vowels = "aeiou"; if (vowels.indexOf(Character.toLowerCase(ch)) != -1) { System.out.println(ch + " is a vowel"); } else { System.out.println(ch + " is a consonant"); } } scanner.close(); } }
Complexity: O(1) time, O(1) space
Time Complexity
The program performs a fixed number of checks regardless of input size, so it runs in constant time O(1).
Space Complexity
Only a few variables are used, so space complexity is constant O(1).
Which Approach is Fastest?
All approaches (if-else, switch-case, string contains) run in constant time; differences are minimal and mostly readability-based.
| Approach | Time | Space | Best For |
|---|---|---|---|
| If-else | O(1) | O(1) | Simple and clear for beginners |
| Switch-case | O(1) | O(1) | Cleaner syntax for multiple fixed cases |
| String contains | O(1) | O(1) | Concise and easy to extend vowel list |