Check Vowel or Consonant in C / C++ / Java / Python / C#

Vowels

There are 26 letters in the English alphabet. Each letter has uppercase and lowercase representation. The uppercase letter A, E, I, O, U and the lowercase letter a, e, i, o, u are the vowels. The remaining letters are consonants. Programs to check if a character is a vowel or consonant are given here.






1. Algorithm to check if a character is a vowel or consonant


1. Take a character x as input.

2. Check if x has an ASCII value that ranges from either 65 to 90 or 97 to 122.

3. If step 2 is false, declare x is neither a vowel nor consonant.

4. If step 2 is true, check if x has the character value equal to any uppercase or lowercase A, E, I, O, U.

5. If step 4 is true, declare x is a vowel.

6. If step 4 is false, declare x is a consonant.




2. Pseudocode to check if a character is a vowel or consonant


Input : A character $x$

Output : $x$ is either a vowel or a consonant

1. Procedure vowelConsonant($x$):

2. If ASCII(x) $\in [65, 90]$ or $[97, 122]$

3. If x $\in [A, E, I, O, U, a, e, i, o, u]$

4. Return "vowel"

5. Else:

6. Return "consonant"

7. Else:

8. Return "neither vowel nor consonant"

9. End Procedure





3. Time complexity to check if a character is a vowel or consonant


Time Complexity: O(1)





4. Flowchart to check if a character is a vowel or consonant

Flowchart to check if a character is a vowel or consonant



5. Program to check if a character is a vowel or consonant

C, C++, Java, Python and C# programs to check if a character is a vowel or consonant have been shown in this section. The programs check the ASCII value of the input character to decide whether it is a member of the alphabet and then matches its character value to uppercase and lowercase A, E, I, O, U.




5.1. C Program to check if a character is a vowel or consonant

Code has been copied
/************************************
  	alphabetacoder.com
C program to check vowel or consonant
*************************************/

#include <stdio.h>

int main() {
    // declare variables
    char x;

    // take input
    printf("Enter the character: ");
    scanf("%c", & x);

    // check if the input charcater
    // belongs to the english alphabet
    // Ascii value of A and Z are 65 and 90 respectively
    // Ascii value of a and z are 97 and 122 respectively
    if ((x >= 65 && x <= 90) || (x >= 97 && x <= 122)) {
        // check for vowel
        // A, a, E, e, I, i, O, o, U, u are vowels
        if (x == 'A' || x == 'a' || x == 'E' || x == 'e' || x == 'I' || x == 'i' || x == 'O' || x == 'o' || x == 'U' || x == 'u')
            printf("%c is a vowel", x);
        else
            printf("%c is a consonant", x);
    } else
        printf("%c is neither a vowel nor a consonant", x);

    return 0;
}

Output


Case 1:

Enter the character: U

U is a vowel


Case 2:

Enter the character: q

q is a consonant


Case 3:

Enter the character: #

# is neither a vowel nor a consonant




5.2. C++ Program to check if a character is a vowel or consonant

Code has been copied
/**************************************
  	  alphabetacoder.com
C++ program to check vowel or consonant
***************************************/

#include <iostream>

using namespace std;

int main() {
    // declare variables
    char x;

    // take input
    cout << "Enter the character: ";
    cin >> x;

    // check if the input charcater
    // belongs to the english alphabet
    // Ascii value of A and Z are 65 and 90 respectively
    // Ascii value of a and z are 97 and 122 respectively
    if ((x >= 65 && x <= 90) || (x >= 97 && x <= 122)) {
        // check for vowel
        // A, a, E, e, I, i, O, o, U, u are vowels
        if (x == 'A' || x == 'a' || x == 'E' || x == 'e' || x == 'I' || x == 'i' || x == 'O' || x == 'o' || x == 'U' || x == 'u')
            cout << x << " is a vowel";
        else
            cout << x << " is a consonant";
    } else
        cout << x << " is neither a vowel nor a consonant";

    return 0;
}

Output


Case 1:

Enter the character: A

A is a vowel


Case 2:

Enter the character: m

m is a consonant


Case 3:

Enter the character: ?

? is neither a vowel nor a consonant




5.3. Java Program to check if a character is a vowel or consonant

Code has been copied
/***************************************
	  alphabetacoder.com
Java program to check vowel or consonant
****************************************/

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // declare variables
        char x;

        // declare instance of Scanner class
        Scanner sc = new Scanner(System.in);

        // take input
        System.out.print("Enter the character: ");
        x = sc.next().charAt(0);

        // check if the input charcater
        // belongs to the english alphabet
        // Ascii value of A and Z are 65 and 90 respectively
        // Ascii value of a and z are 97 and 122 respectively
        if ((x >= 65 && x <= 90) || (x >= 97 && x <= 122)) {
            // check for vowel
            // A, a, E, e, I, i, O, o, U, u are vowels
            if (x == 'A' || x == 'a' || x == 'E' || x == 'e' || x == 'I' || x == 'i' || x == 'O' || x == 'o' || x == 'U' || x == 'u')
                System.out.println(x + " is a vowel");
            else
                System.out.println(x + " is a consonant");
        } else
            System.out.println(x + " is neither a vowel nor a consonant");
    }
}

Output


Case 1:

Enter the character: e

e is a vowel


Case 2:

Enter the character: s

s is a consonant


Case 3:

Enter the character: /

/ is neither a vowel nor a consonant




5.4. Python Program to check if a character is a vowel or consonant

Code has been copied
# ******************************************
#          alphabetacoder.com
# Python program to check vowel or consonant
# ******************************************

# take input
x = input("Enter the character: ")

# declare the list of vowel
vowel = ["A", "E", "I", "O", "U", "a", "e", "i", "o", "u"]

# check if the input charcater
# belongs to the english alphabet
# Ascii value of A and Z are 65 and 90 respectively
# Ascii value of a and z are 97 and 122 respectively
if (ord(x) >= 65 and ord(x) <= 90) or (ord(x) >= 97 and ord(x) <= 122):
    # check for vowel
    # A, a, E, e, I, i, O, o, U, u are vowels
    if x in vowel:
        print(x, "is a vowel")
    else:
        print(x, "is a consonant")
else:
    print(x, "is neither a vowel nor a consonant")

Output


Case 1:

Enter the character: O

O is a vowel


Case 2:

Enter the character: K

K is a consonant


Case 3:

Enter the character: )

) is neither a vowel nor a consonant




5.5. C# Program to check if a character is a vowel or consonant

Code has been copied
/****************************************
	   alphabetacoder.com
 C# program to check vowel or consonant
****************************************/

using System;

namespace VowelConsonant {
    class Program {
        static void Main(string[] args) {
            // declare variables
            char x;

            // take input
            Console.Write("Enter the character: ");
            x = Console.ReadLine()[0];

            // check if the input charcater
            // belongs to the english alphabet
            // Ascii value of A and Z are 65 and 90 respectively
            // Ascii value of a and z are 97 and 122 respectively
            if ((x >= 65 && x <= 90) || (x >= 97 && x <= 122)) {
                // check for vowel
                // A, a, E, e, I, i, O, o, U, u are vowels
                if (x == 'A' || x == 'a' || x == 'E' || x == 'e' || x == 'I' || x == 'i' || x == 'O' || x == 'o' || x == 'U' || x == 'u')
                    Console.WriteLine(x + " is a vowel");
                else
                    Console.WriteLine(x + " is a consonant");
            } else
                Console.WriteLine(x + " is neither a vowel nor a consonant");

            // wait for user to press any key
            Console.ReadKey();
        }
    }
}

Output


Case 1:

Enter the character: I

I is a vowel


Case 2:

Enter the character: w

w is a consonant


Case 3:

Enter the character: =

= is neither a vowel nor a consonant