Vowels and consonants
In the English alphabet, a,e,i,o,u are considered as vowels. All the other alphabets are called consonants.
Among 26 English alphabets, 5 are vowels and 21 are consonants.
Write a program in C++ to input a character from the user and check whether it is a vowel or a consonant using the switch statement.
We have to input a character from the user. We compare the entered character with the vowels. If it matches with any of the vowels, then we will print it is a vowel message. Else we will print it is a consonant message. We are using the switch statement which is conditional control statement. It is used when we have multiple cases for a single input.
The syntax of Switch statement:
switch (conditional variable)
{
case value 1:
statement;
break;
case value 2:statement;
break;
.
.
.
default:
statement;
}
Break is a jump statement which transfers the control of execution out of the loop. It reduces the extra time which is consumed in checking remaining false condition, even after the true statement is already found.
Program
#include<iostream.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
cout<<"PROGRAM TO CHECK A VOWEL OR CONSONANT";
cout<<"\nEnter a character=";
cin>>ch;
switch(ch)
{
case 'a':
cout<<"It is a vowel";
break;
case 'e':
cout<<"It is a vowel";
break;
case 'i':
cout<<"It is a vowel";
break;
case 'o':
cout<<"It is a vowel";
break;
case 'u':
cout<<"It is a vowel";
break;
default:
cout<<"It is a consonant";
break;
}
getch();
}
#include<conio.h>
void main()
{
char ch;
clrscr();
cout<<"PROGRAM TO CHECK A VOWEL OR CONSONANT";
cout<<"\nEnter a character=";
cin>>ch;
switch(ch)
{
case 'a':
cout<<"It is a vowel";
break;
case 'e':
cout<<"It is a vowel";
break;
case 'i':
cout<<"It is a vowel";
break;
case 'o':
cout<<"It is a vowel";
break;
case 'u':
cout<<"It is a vowel";
break;
default:
cout<<"It is a consonant";
break;
}
getch();
}
Output
CHECKING VOWEL AND CONSONANT |
Explanation
We have entered the character i which is a vowel and matches the third case of the switch case. Then the break statement brings the control of execution out of the switch loop. This reduces the program execution time. If we enter any of the character other than vowel then the default case becomes true and it is a consonant message will be displayed on the screen.
Fact about C++
The advantage in C++ over C is that variables can be declared anywhere within the program. It must be declared just before the using the variable for the first time.
No comments:
Post a Comment