Check Character is Vowel or Consonant

Definition & Explanation

Heading

Desc

C

Method 1 :

#include <stdio.h>
#include <ctype.h>

int isVowel(char c) {
    c = tolower(c); // Convert to lowercase for uniform comparison
    return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
}

int main() {
    char character;
    printf("Enter a character: ");
    scanf("%c", &character);

    if (isalpha(character)) { // Check if input is an alphabet
        if (isVowel(character))
            printf("The character '%c' is a vowel.\n", character);
        else
            printf("The character '%c' is a consonant.\n", character);
    } else {
        printf("Invalid input. Please enter an alphabet.\n");
    }

    return 0;
}

Output :

C++

Method 1 :

#include <iostream>
using namespace std;

bool isVowel(char c) {
    c = tolower(c); // Convert to lowercase for uniform comparison
    return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
}

int main() {
    char character;
    cout << "Enter a character: ";
    cin >> character;

    if (isalpha(character)) { // Check if input is an alphabet
        if (isVowel(character))
            cout << "The character '" << character << "' is a vowel." << endl;
        else
            cout << "The character '" << character << "' is a consonant." << endl;
    } else {
        cout << "Invalid input. Please enter an alphabet." << endl;
    }

    return 0;
}

Output :

JAVA

Method 1 :

import java.util.Scanner;

public class VowelConsonantChecker {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a character: ");
        char character = scanner.next().charAt(0);

        if (Character.isLetter(character)) {
            char lowerCaseChar = Character.toLowerCase(character);
            if (lowerCaseChar == 'a' || lowerCaseChar == 'e' || lowerCaseChar == 'i' ||
                lowerCaseChar == 'o' || lowerCaseChar == 'u') {
                System.out.println("The character '" + character + "' is a vowel.");
            } else {
                System.out.println("The character '" + character + "' is a consonant.");
            }
        } else {
            System.out.println("Invalid input. Please enter an alphabet.");
        }

        scanner.close();
    }
}

Output :

Python

Method 1 :

def isvowel(c):
     c=c.upper()
     return c=='A' or c=='E' or c=='I' or c=='O' or c=='U'

c='f'

if not c.isalpha():
     print("Not a alphabet")
elif isvowel(c):
     print("it is a Vowel")
else:
     print("It is a Consonent")

Output :

It is a Consonent