C++ Program to Check Whether a Character is a Vowel, Consonant or Digit

«
»

This is a C++ Program to Check if a Character is a Vowel, Consonant or Digit.

Problem Description

The program takes a character and checks if it is a vowel, consonant or a digit.

Problem Solution

1. A character is entered.
2. Using nested if else condition, the character is checked if it is a vowel, consonant or a digit.
3. The result is printed.
4. Exit.

C++ Program/Source code

Here is the source code of C++ Program to Check if a Character is a Vowel, Consonant or Digit. The program output is shown below.

  1. #include<iostream>
  2. #include<ctype.h>
  3. using namespace std;
  4. int main ()
  5. {
  6.     char ch;
  7.     cout << "Enter a character : ";
  8.     cin >> ch;
  9.     if (isdigit(ch))
  10.         cout << "\nThe entered character is a digit.";
  11.     else if (isalpha(ch))
  12.         {
  13.             if ((ch == 'A') || (ch == 'E')|| (ch == 'I')|| (ch == 'O')|| (ch == 'U')|| (ch == 'a')|| (ch == 'e')|| 
  14.             (ch == 'i')|| (ch == 'o')|| (ch == 'u'))
  15.                 cout << "\nThe entered character is a vowel.";
  16.             else
  17.                 cout << "\nThe entered character is a consonant.";
  18.         }
  19.         else 
  20.             cout << "\nThe entered character is a special character.";
  21.     return 0;
  22. }
Program Explanation

1. The user is asked to enter a character and is stored in the variable ‘char’.
2. The character is checked if it is a vowel, consonant or digit using functions under the library ctype.h.
3. If it is neither of the three, then the entered character is a special character,
4. The result is then printed.

Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement
advertisement
Runtime Test Cases
Case 1 :
Enter a character : I
The entered character is a vowel.
 
Case 2 :
Enter a character : /
The entered character is a special character.
 
Case 3 :
Enter a character : 8 
The entered character is a digit.

Sanfoundry Global Education & Learning Series – C++ Programs.

To practice all C++ programs, here is complete set of 1000+ C++ Programming examples.

Take C++ Programming Mock Tests - Chapterwise!
Start the Test Now: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.