Programs

C++ Program to Check Whether a character is Vowel or Consonant

C++ Program to Check Whether a character is Vowel or Consonant

In this program, we’ll prompt the user to input a character and then we’ll check whether the character is a vowel or a consonant. Note that we’ll consider both uppercase and lowercase letters, and assume that the input is a letter of the alphabet.

Here’s the code written for Turbo C++:

#include <iostream.h>
#include <conio.h>

void main() {
    clrscr();  // Clear the screen (specific to Turbo C++)

    char ch;

    cout << "Enter a character: ";
    cin >> ch;

    // Convert the character to uppercase to simplify checking
    ch = toupper(ch);

    // Check if the character is a vowel or a consonant
    if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
        cout << "The character is a vowel." << endl;
    } else if ((ch >= 'A' && ch <= 'Z')) {
        cout << "The character is a consonant." << endl;
    } else {
        cout << "Invalid input. Please enter an alphabet letter." << endl;
    }

    getch();  // Wait for user input (specific to Turbo C++)
}

To compile and run the program:

  1. Open Turbo C++.
  2. Create a new file and copy-paste the above code into it.
  3. Save the file.
  4. Compile the code (usually by pressing Alt + F9).
  5. Run the program (usually by pressing Ctrl + F9).

The program will prompt you to enter a character, and then it will display whether the character is a vowel, a consonant, or an invalid input. Note that toupper() is used to convert the character to uppercase, which simplifies the checking process.