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
- Start the program.
- Prompt the user to input a single character.
- Read the character from the user.
- Check if the character is a vowel (a, e, i, o, u, A, E, I, O, U).
- If it is a vowel, display that it is a vowel.
- If it is not a vowel, display that it is a consonant.
- 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.)