In this, we are going to write a simple program to check if a alphabet character input by user is a vowel or consonant or if he has given an invalid character in C++.
We first check if the input character is an alphabet or not. If it's an alphabet then we compare the input if its one of the five vowels of both upper and lower case and print the result accordingly
The code given below can be used for TURBO C++ Compiler:-
// check character is vowel OR consonant ?
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
char ch;
cout << "Enter character to find if it's vowel or consonant\n";
cin >> ch;
// to check if character entered is an alphabet
// with help of ascii values of alphabets
if ((int(ch) >= 97 && int(ch) <= 122) || (int(ch) >= 65 && int(ch) <= 90))
{
// if character is actually alphabet,
// check if its A,a,E,e,I,i,O,o,U,u
if (ch == 'A' || ch == 'a' || ch == 'E' || ch == 'e' || ch == 'I' || ch == 'i' || ch == 'O' || ch == 'o' || ch == 'U' || ch == 'u')
{
cout << "You entered VOWEL ";
}
// if alphabet is not vowel its consonent
else
{
cout << "You entered CONSONANT ";
}
}
else
{
cout << "INVALID character";
}
getch();
}
The code given below can be used for g++/gcc Compiler:-
// check character is vovel OR consonant ?
#include <iostream>
using namespace std;
int main()
{
char ch;
cout << "Enter character to find if it's vovel or consonant\n";
cin >> ch;
// to check if character entered is an alphabet
// with help of ascii values of alphabets
if ((int(ch) >= 97 && int(ch) <= 122) || (int(ch) >= 65 && int(ch) <= 90))
{
// if character is actually alphabet,
// check if its A,a,E,e,I,i,O,o,U,u
if (ch == 'A' || ch == 'a' || ch == 'E' || ch == 'e' || ch == 'I' || ch == 'i' || ch == 'O' || ch == 'o' || ch == 'U' || ch == 'u')
{
cout << "You entered VOWEL ";
}
// if alphabet is not vovel its consonent
else
{
cout << "You entered CONSONANT ";
}
}
else
{
cout << "INVALID character";
}
return 0;
}
#ENJOYCODING
Â
Post a Comment
FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP