Python/C/C++/JAVA

Basic Practice Programs with Code and Concept

By D.S

Character is Vowel or Consonant??

In this program, you will be prompted to enter a character, and the program will determine whether it is a vowel or a consonant.

(a.) C Code

#include <stdio.h>

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

        if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
            ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
            printf("%c is a vowel.
", ch);
        } else {
            printf("%c is a consonant.
", ch);
        }

        return 0;
    }
Output:-
Enter a character: a
a is a vowel.

(b.) C++ Code

#include <iostream>

    using namespace std;

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

        if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
            ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
            cout << ch << " is a vowel." << endl;
        } else {
            cout << ch << " is a consonant." << endl;
        }

        return 0;
    }
Output:-
Enter a character: a
a is a vowel.

(c.) Python Code

def main():
        ch = input("Enter a character: ")
        if ch.lower() in ['a', 'e', 'i', 'o', 'u']:
            print(ch, "is a vowel.")
        else:
            print(ch, "is a consonant.")

    if __name__ == "__main__":
        main()
Output:-
Enter a character: a
a is a vowel.

(d.) Java Code

import java.util.Scanner;

    public class CheckVowelConsonant {
        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 (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
                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.");
            }
        }
    }
Output:-
Enter a character: a
a is a vowel.