C++ Program to Check Whether a character is Vowel or Consonant (When character input in alphabet only)

by | Jan 9, 2023 | C++, C++ Programs

Introduction

This program will check if the user’s entered character (alphabet) is a vowel or a consonant.

To understand this example better, you must have gone through these topics of C++ language

1. Operators

2. if-else statement

Vowels are a, e, i, o, u, in lower case and A, E, I, O, and U in Upper case.

All other alphabets, including lower case and upper case, except vowels, are consonants.

Program

#include<iostream>
using namespace std;
int main()
{
char sValue;
cout<<"Enter a character: "<<endl;
cin>>sValue;


if ( (sValue=='a' || sValue=='e'|| sValue=='i'|| sValue=='o'|| sValue=='u') ||
(sValue=='A' || sValue=='E'|| sValue=='I'|| sValue=='O'|| sValue=='U') ) // '||'(or) operator is used
{
cout<<sValue<<" is a vowel."<<endl;
}
else
{
cout<<sValue<<" is a consonent"<<endl;
}
return 0;
}

 

Output

Explanation

In this program, we first declare a ‘char’ variable ‘sValue’ to store the character value entered by the user.

cout<<“Enter a character: “<<endl;

A message is displayed on the screen using ‘cout’ for the user to enter a character value, and ‘endl’ is used to take a new line.

cin>>sValue;

The value entered by the user gets stored in ‘sValue’ using ‘cin’.

if ( (sValue==’a’ || sValue==’e’|| sValue==’i’|| sValue==’o’|| sValue==’u’) ||

(sValue==’A’ || sValue==’E’|| sValue==’I’|| sValue==’O’|| sValue==’U’) )

The condition is written inside the parenthesis of ‘if’ and states that if the value of ‘sValue’ is equal to either of the character mentioned in the statement, then it will be considered a vowel.

That’s we have use OR (||) operator to check the validity of the condition.

The value of ‘sValue’ entered by the user is ‘t’ and it doesn’t satisfy the condition written of ‘if’ statement thus, else part of the statement is executed.

 

Author

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.