Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors

Program determines character is a vowel or consonant

Write a program that takes a character as input and determines whether it is a vowel or consonant.

Program in C

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

int main() {
    char ch;

    printf("Enter a character: ");
    scanf("%c", &ch);

    // Converting to lowercase to handle both uppercase and lowercase characters
    ch = tolower(ch);

    if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
        printf("%c is a vowel.\n", ch);
    } else {
        printf("%c is a consonant.\n", ch);
    }

    return 0;
}

Explanation:

  • In this program, we first declare a character variable ch.
  • We prompt the user to enter a character using printf, and then we use scanf to read the character into the ch variable.
  • To handle both uppercase and lowercase characters, we convert the character to lowercase using the tolower function.
  • We then use an if statement to check if the character is equal to any of the vowel characters ‘a’, ‘e’, ‘i’, ‘o’, or ‘u’. If it is, we print that it is a vowel.
  • Otherwise, we print that it is a consonant.
  • Finally, we return 0 to indicate successful execution of the program.

Output:

Output
Enter a character: e
e is a vowel.

Program in Java

Java
import java.util.Scanner;

public class VowelConsonantChecker {
    public static void main(String[] args) {
        char ch;

        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a character: ");
        ch = scanner.next().charAt(0);

        // Converting to lowercase to handle both uppercase and lowercase characters
        ch = Character.toLowerCase(ch);

        if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
            System.out.println(ch + " is a vowel.");
        } else {
            System.out.println(ch + " is a consonant.");
        }

        scanner.close();
    }
}

Explanation:

Java Output
Enter a character: a
a is a vowel.