Header Ads

Write a program to find whether a given character is a vowel or a consonant



A vowel is a speech sound pronounced without any significant obstruction of the airflow in the vocal tract. In the English alphabet, the five letters a, e, i, o, and u (and sometimes y) are considered vowels. These letters can represent various vowel sounds that are used to form syllables and words.

A consonant is a speech sound produced by obstructing the air flow in the vocal tract. In the English alphabet, all letters except the five vowels (a, e, i, o, and u) are considered consonants. These letters can represent various consonant sounds, including sounds made with the lips, teeth, tongue, and voice. Some examples of consonant sounds in English are b, c, d, f, g, and h.


This is a program written in C++ that takes a character as input and determines whether it's a vowel or a consonant.

The program uses the toupper() function from the cctype library to convert the input character to uppercase, in case it was entered in lowercase.

It then checks if the uppercase character is one of the five vowels (A, E, I, O, U) and outputs "Vowel".

If the character is not one of the vowels or not a character (i.e. not between 'A' and 'Z'), the program outputs "Not a Character".

Otherwise, it outputs "Consonant".


Code :


#include <iostream>
#include <cctype>
using namespace std;

int main()
{
char c;
cout << "Enter a Character : ";
cin >> c;
c = toupper(c);
if (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U')
{
cout << "Vowel" << endl;
}
else if (c < 'A' || c > 'Z')
{
cout << "Not a Character" << endl;
}
else
{
cout << "Consonant" << endl;
}

return 0;
}


Output :

Test 1:

Enter a Character : A
Vowel

-----------------------------
Test 2:

Enter a Character : 4
Not a Character

-----------------------------
Test 3:

Enter a Character : a
Vowel

-----------------------------

Explanation :


This is a program written in C++ that determines if a character entered by the user is a vowel or a consonant.

1. The code starts by including the necessary libraries, iostream for input/output operations and cctype for character operations.

2. The using namespace std statement allows the use of standard C++ library names without adding std:: before them.

3. In the main function, a character variable c is declared.

4. The program prompts the user to enter a character by using cout and stores the entered character in c using cin.

5. The toupper() function from the cctype library is used to convert the entered character to uppercase.

6. An if statement checks if the uppercase character is equal to one of the five vowels (A, E, I, O, U). If the condition is true, the program outputs "Vowel".

7. Another if statement checks if the uppercase character is not between 'A' and 'Z'. If the condition is true, the program outputs "Not a Character".

8. If none of the conditions in the if statements are met, the program outputs "Consonant".

9. The return 0 statement at the end of the main function indicates that the program has run successfully.





Related Links :











Post a Comment

0 Comments