Program to find if a character is vowel or Consonant

Last Updated : 28 Apr, 2026

Given a character ch representing an English alphabet, determine whether it is a vowel or not. Return true if ch is a vowel otherwise return false.

Examples:  

Input: ch = 'a'
Output: true
Explanation: 'a' is a vowel. So output for this test case is true.

Input: ch = 'Z'
Output: false
Explanation: 'Z' is not a vowel. So output for this test case is false.

Try It Yourself
redirect icon
vowel_or_Consonant

Using Direct Character Comparison (Handling Uppercase & Lowercase) - O(1) Time O(1) Space

The Idea is to Directly compare the given character with all vowels (both lowercase and uppercase). If it matches any, return true; otherwise return false.

Algorithm:

  • Take input character ch.
  • Check if ch is equal to 'a', 'e', 'i', 'o', 'u' or 'A', 'E', 'I', 'O', 'U'.
  • If any condition matches -> return true.
  • Otherwise -> return false.
C++
// C++ program to check if a given character
// is vowel or consonant.
#include <iostream>
using namespace std;

// Function to check whether a character is
// vowel or not
bool isVowel(char ch)
{
    // Check if the character matches any lowercase or uppercase vowel
    if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' ||
        ch == 'u' || ch == 'A' || ch == 'E' ||
        ch == 'I' || ch == 'O' || ch == 'U')
        return true;
    else
        return false;
}

// Driver code
int main() {
    char ch = 'a'; 
    cout << boolalpha << isVowel(ch) << endl; 
    return 0;
}
C
#include <stdio.h>
#include <stdbool.h>

// Function to check whether a character is vowel or not
bool isVowel(char ch) {
    
    // Check if the character matches any lowercase or
    // uppercase vowel
    if (ch == 'a' || ch == 'e' || ch == 'i' || 
        ch == 'o' || ch == 'u' || ch == 'A' || 
        ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')
        return true;
    else
        return false;
}

// Driver code
int main() {
    char ch = 'a';
    printf("%s\n", isVowel(ch) ? "true" : "false");
    return 0;
}
Java
/* Java program to check if a given character
 is vowel or consonant.*/
public class GfG {
    
    // Function to check whether a character is
    // vowel or not
    static boolean isVowel(char ch) {
        
        // Check if the character matches any lowercase or 
        // uppercase vowel
        if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' ||
            ch == 'u' || ch == 'A' || ch == 'E' ||
            ch == 'I' || ch == 'O' || ch == 'U')
            return true;
        else
            return false;
    }

    public static void main(String[] args) {
        char ch = 'a';
        System.out.println(isVowel(ch));
    }
}
Python
"""
Python program to check if a given character
is vowel or consonant.
"""

# Function to check whether a character is vowel or not
def isVowel(ch):
    
    # Check if the character matches any lowercase or 
    # uppercase vowel
    if (ch == 'a' or ch == 'e' or ch == 'i' or ch == 'o' or
        ch == 'u' or ch == 'A' or ch == 'E' or
        ch == 'I' or ch == 'O' or ch == 'U'):
        return True
    else:
        return False

# Driver code
ch = 'a'
print(isVowel(ch))
C#
// C# program to check if a given character
// is vowel or consonant.
using System;

class GfG
{
    // Function to check whether a character is
    // vowel or not
    static bool isVowel(char ch)
    {
        // Check if the character matches any lowercase or uppercase vowel
        if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' ||
            ch == 'u' || ch == 'A' || ch == 'E' ||
            ch == 'I' || ch == 'O' || ch == 'U')
            return true;
        else
            return false;
    }

    static void Main()
    {
        char ch = 'a';
        Console.WriteLine(isVowel(ch));
    }
}
JavaScript
// Function to check whether a character is
// vowel or not
function isVowel(ch) {
    
    // Check if the character matches any lowercase or uppercase vowel
    if (ch === 'a' || ch === 'e' || ch === 'i' || ch === 'o' ||
        ch === 'u' || ch === 'A' || ch === 'E' ||
        ch === 'I' || ch === 'O' || ch === 'U')

        // If match found, it is a vowel
        return true;
    else
        // Otherwise, it is not a vowel
        return false;
}

// Driver code
let ch = 'a';
console.log(isVowel(ch));

Output
true

Time Complexity: O(1)
Auxiliary Space: O(1)

Using Bitmasking Technique - O(1) Time O(1) Space

Both lowercase and uppercase vowels share the same last 5 bits (LSBs) in their ASCII representation. This allows us to normalize any character using (ch & 0x1F) and map it to a small range of values.

We use a precomputed bitmask 0x208222, which is designed such that when it is right-shifted by the normalized value (ch & 0x1F), it produces 1 at the least significant bit for vowel positions (1, 5, 9, 15, 21), and 0 for all other characters.

Thus, by evaluating:

( 0 x 208222 >> (ch & 0 x 1F)) & 1

ASCII Values of Vowels (Lowercase and Uppercase):

Values_of_Vowels

Algorithm:

  • Take input character ch.
  • Compute (ch & 0x1F) to get last 5 bits.
  • Right shift 0x208222 by this value.
  • Apply & 1 to extract the result.
  • If result is 1 → return true (vowel).
  • Else → return false (not a vowel).
C++
#include <iostream>
using namespace std;

// Function to check whether a character is vowel or not
// using bitmasking technique
bool isVowel(char ch) {
    
    // Right shift the bitmask by (ch & 0x1f) and check LSB
    // If result is 1 → vowel, else → not a vowel
    return (0x208222 >> (ch & 0x1f)) & 1;
}

// Driver code
int main() {
    char ch = 'a';  
    cout << boolalpha << isVowel(ch) << endl;
    return 0;
}
C
#include <stdio.h>

// Function to check whether a character is vowel or not
// using bitmasking technique
int isVowel(char ch) {
    
    // Right shift the bitmask by (ch & 0x1f) and check LSB
    // If result is 1 → vowel, else → not a vowel
    return (0x208222 >> (ch & 0x1f)) & 1;
}

// Driver code
int main() {
    char ch = 'a';
    printf("%s\n", isVowel(ch) ? "true" : "false");
    return 0;
}
Java
public class GfG {
    // Function to check whether a character is vowel or not
    // using bitmasking technique
    public static boolean isVowel(char ch) {
        
        // Right shift the bitmask by (ch & 0x1f) and check LSB
        // If result is 1 → vowel, else → not a vowel
        return ((0x208222 >> (ch & 0x1f)) & 1) == 1;
    }

    public static void main(String[] args) {
        char ch = 'a';
        System.out.println(isVowel(ch));
    }
}
Python
class Solution:
    def isVowel(self, ch):
        # Right shift the bitmask by (ch & 0x1f) and check LSB
        return (0x208222 >> (ord(ch) & 0x1f)) & 1 == 1

# Driver code
obj = Solution()
ch = 'a'
print(obj.isVowel(ch))
C#
using System;

public class GfG {
    // Function to check whether a character is vowel or not
    // using bitmasking technique
    public static bool IsVowel(char ch) {
        // Right shift the bitmask by (ch & 0x1f) and check LSB
        // If result is 1 → vowel, else → not a vowel
        return ((0x208222 >> (ch & 0x1f)) & 1) == 1;
    }

    public static void Main() {
        char ch = 'a';
        Console.WriteLine(IsVowel(ch));
    }
}
JavaScript
// Function to check whether a character is vowel or not
// using bitmasking technique
function isVowel(ch) {
    
    // Right shift the bitmask by (ch.charCodeAt(0) & 0x1f) and check LSB
    // If result is 1 → vowel, else → not a vowel
    return ((0x208222 >> (ch.charCodeAt(0) & 0x1f)) & 1) === 1;
}

// Driver code
let ch = 'a';
console.log(isVowel(ch));

Output
true

Time Complexity: O(1)
Auxiliary Space: O(1)

Comment