C++ program to Check Vowel or consonant using switch case

C++ program to Check Vowel or consonant using switch case

In this article, we will discuss the concept of the C++ program to Check Vowel or consonant using switch case statements

In this post, we are going to learn how to check the vowels and consonants using switch statements in the C++  programming language

Check vowel and consonants

Check Vowel or consonant using switch case statements with the break

The program allows to enter an Alphabet and it checks and displays whether the given alphabet is vowel or consonant with the break statements

Program 1

#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
    char ch;
    cout<<"Enter any Alpabet\n"; //input alphabet from user
    cin>>ch;//store the Entered Alphabet in ch

    switch(ch){
    //check lower case vowel letters
    case 'a':
    cout<<ch<<" is a vowel";
    break;

    case 'e':
    cout<<ch<<" is a vowel";
    break;

    case 'i':
    cout<<ch<<" is a vowel";
    break;

    case 'o':
    cout<<ch<<" is a vowel";
    break;

    case 'u':
    cout<<ch<<" is a vowel";
    break;

    //check upper case vowel letters
    case 'A':
    cout<<ch<<" is a vowel";
    break;

    case 'E':
    cout<<ch<<" is a vowel";
    break;

    case 'I':
    cout<<ch<<" is a vowel";
    break;

    case 'O':
    cout<<ch<<" is a vowel";
    break;

    case 'U':
    cout<<ch<<" is a vowel";
    break;

    default:
    cout<<ch<<" is a consonant";
    break;
    }
    getch();

    return 0;
    }

When the above code is executed, it produces the following result

case 1

Enter any Alphabet
u
u is a vowel


case 2

Enter any Alphabet
I
I is a vowel

 

case 3

Enter any Alphabet
N
N is a consonant

 

case 4

Enter any Alphabet
d
d is a consonant

Approach

  • Define a character variable char ch
  • The program is asked the user to enter an Alphabets for check whether a vowel or a consonant
  • Entered character is stored in the ch variable
  • Define cases for the character ch with vowel character both capital and small
  • Next, the program checks every case using the given character.
  • Finally, it displays whether the given character is vowel or consonant

 


Check Vowel or consonant using switch case statements without the break

The program allows to enter an Alphabet and it checks and displays whether the given alphabet vowel or consonant without the break statements

Program 2

#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
    char ch;
    cout<<"Enter any Alpabet\n"; //input alphabet from user
    cin>>ch;//store the Entered Alphabet in ch

    switch(ch){
    //check lower case vowel letters
    case 'a':
    case 'e':
    case 'i':
    case 'o':
    case 'u':

    //check upper case vowel letters
    case 'A':
    case 'E':
    case 'I':
    case 'O':
    case 'U':
    cout<<ch<<" is a vowel";
    break;

    default:
    cout<<ch<<" is a consonant";
    break;
    }
    getch();

    return 0;
    }

 

When the above code is executed, it produces the following result

case 1

Enter any Alphabet
i
i is a vowel

 

case 2

Enter any Alphabet
U
U is a vowel

 

case 3

Enter any Alphabet
H
H is a consonant


case 4

Enter any Alphabet
b
b is a consonant

 

Approach

  • Define a character variable ch to check the entered character
  • The program is asked the user to enter an Alphabets for check whether a vowel or a consonant
  • Entered character is stored in the ch variable
  • Define cases for the character ch with vowel character both capital and small
  • Next, the program checks every case using the given character.
  • Finally, it displays whether the given character is vowel or consonant

 

Suggested for you

Data type in C++ language

Variable in C++ language

The operator in C++ language

Switch case statements in C++ language

 

Similar post

Java program to check Vowel or consonant using switch case statements

C program to check Vowel or consonant using switch case statements

Java program to check Vowel or consonant using switch case statements
C program to check Vowel or consonant using switch case
C++ programsCpp language