c++ program to check whether a character is vowel or consonant |
We all know that decision making is the backbone of programming. Whether you want to add a new feature or boost the security of your program, you use conditional statements to implement your logic. In this post, we will discuss c++ program to check whether a character is vowel or consonant using switch, if-else-if, and compound conditional statements.
c++ program to check whether a character is vowel or consonant using switch statement
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | #include <iostream> using namespace std; int main() { char alpha; cout << "enter an alphabet" << endl; cin >> alpha; switch (alpha) {//switch expression //switch cases case 'a': cout << alpha << " is a vowel" << endl; break; case 'e': cout << alpha << " is a vowel" << endl; break; case 'i': cout << alpha << " is a vowel" << endl; break; case 'o': cout << alpha << " is a vowel" << endl; break; case 'u': cout << alpha << " is a vowel" << endl; break; default: cout <<alpha <<" is a consonant" << endl;//default case break; } system("pause"); return 0; } |
Variable alpha is declared to take input from the user. This variable is switched to match five cases of the switch statement. If the character matches any of the five cases then the program prints alpha as a vowel. If there is no match, the default case executes and displays alpha as a consonant. Now we are going to write the same program using if-else-if ladder.
c++ program to check whether a character is vowel or consonant using if-else-if ladder
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #include <iostream> using namespace std; int main() { char alpha; cout << "enter an alphabet" << endl; cin >> alpha; if(alpha=='a') cout << alpha << " is a vowel" << endl; else if (alpha == 'e') cout << alpha << " is a vowel" << endl; else if (alpha == 'i') cout << alpha << " is a vowel" << endl; else if (alpha == 'o') cout << alpha << " is a vowel" << endl; else if (alpha == 'u') cout << alpha << " is a vowel" << endl; else cout <<alpha <<" is a consonant" << endl; system("pause"); return 0; } |
We are again using alpha as the variable to take input from the user. But we are using if-else-if ladder to write the same program this time. The if and else-if conditions are used to validate the character entered by the user. If any of the if or else-if conditions become true, the program prints alpha as a vowel. Else, alpha is declared as a consonant.
c++ program to check whether a character is vowel or consonant using compound conditional statements
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include <iostream> using namespace std; int main() { char alpha; cout << "enter an alphabet" << endl; cin >> alpha; if(alpha=='a'|| alpha == 'e'|| alpha == 'i'|| alpha == 'o'|| alpha == 'u') cout << alpha << " is a vowel" << endl; else cout <<alpha <<" is a consonant" << endl; system("pause"); return 0; } |
Variable alpha is the variable used for taking input again. But this time we are implementing the same program using compound conditional statement. Multiple conditions are applied using an OR logical operator. If any of these conditions become true, the program will print alpha as a vowel. If all these conditions are false then alpha is displayed as a consonant.
Switch vs if-else-if ladder vs using compound conditional statements
As we have written a program using these three methods, you can yourself decide which method is best for what. Switch statements are very useful in circumstances when you want to create menus, dealing with fixed data values, etc. Switch statements are better than if-else-if ladder in such circumstances. But in scenarios where a comparison is required, we can not use switch statements. For multiple comparisons, we either use if-else-if ladder or compound conditional statements. Compound conditional statements have many benefits over if-else-if ladder because it not only reduces the lines of code but also make the code manageable, readable, and brief.
Related posts
c++ program to find largest number among three numbers
Related posts
c++ program to find largest number among three numbers
0 Comments