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.

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.

Free 30-Day Java Certification Bootcamp is Live. Join Now!

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
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 40s–60s and exploring new directions in your career, I also offer mentoring. Learn more here.