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

Write a C++ program to accept a single character input from the user and determine whether it is a vowel or a consonant. 

Input and Output Examples

  • Input: a

    Output: a is a vowel.

  • Input: z

    Output: z is a consonant.

Algorithm to Check if a Character is a Vowel or Consonant

  1. Start the program.
  2. Prompt the user to input a single character.
  3. Read the character from the user.
  4. Check if the character is a vowel (a, e, i, o, u, A, E, I, O, U).
  5. If it is a vowel, display that it is a vowel.
  6. If it is not a vowel, display that it is a consonant.
  7. End the program.

Below is the implementation of the program using if-else conditions. 

cpp
#include <iostream> 
using namespace std; 

int main() {
    // Declare a variable to store the character
    char ch;
    
    // Prompt the user to enter a character
    cout << "Enter a character: ";
    cin >> ch;

    // Check if the character is a vowel
    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 {
        // If it is not a vowel, it is a consonant
        cout << ch << " is a consonant." << endl;
    }

    // Ending the program
    return 0;
}

Testing the Program with Different Input Values

To ensure robustness, it is good practice to test our program with various types of inputs:

  • Input: E
  • Output: E is a vowel.
  • Input: k
  • Output: k is a consonant.
  • Input: 9
  • Output: 9 is a consonant. (Note: While numbers are not consonants, this program treats any non-vowel character as a consonant for simplicity.)

Programming

8389

204

Related Articles