Codeforcoding

Java program to check Vowel or consonant using switch case statements

Java program to check Vowel or consonant using switch case

In this article, we will discuss the concept of the Java 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 Java  programming language

Java program to check Vowel or consonant using switch case statements
Vowel and Consonant

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

import java.util.Scanner;
public class CheckVowelSwitchcase{
public static void main(String args[]){
    char ch;//variable declaration

 Scanner scan=new Scanner(System.in); 
    //create a scanner object for input
    
System.out.println("Enter the Alphabet for check vowel or consonant  ");
ch=scan.next().charAt(0);;// store the input from the user

switch(ch){
    //check lower case vowel letters
    case 'a':
    System.out.println(ch+" is a vowel");
    break;
    
    case 'e':
    System.out.println(ch+" is a vowel");
    break;
    
    case 'i':
    System.out.println(ch+" is a vowel");
    break;
    
    case 'o':
    System.out.println(ch+" is a vowel");
    break;
    
    case 'u':
    System.out.println(ch+" is a vowel");
    break;
    
    //check upper case vowel letters
    case 'A':
    System.out.println(ch+" is a vowel");
    break;
    
    case 'E':
    System.out.println(ch+" is a vowel");
    break;
    
    case 'I':
    System.out.println(ch+" is a vowel");
    break;
    
    case 'O':
    System.out.println(ch+" is a vowel");
    break;
    
    case 'U':
    System.out.println(ch+" is a vowel");
    break;
    
    default:
    System.out.println(ch+" is a consonant");
    break;
    
    
}
}
    
}

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

Case 1

Enter the Alphabet for check vowel or consonant
a
a is a vowel


Case 2

Enter the Alphabet for check vowel or consonant
U
U is a vowel


Case 3

Enter the Alphabet for check vowel or consonant
k
k is a consonant

 

Case 4

Enter the Alphabet for check vowel or consonant
M
M is a consonant


Approach

 

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 is vowel or consonant without the break statements

Program 2

import java.util.Scanner;
public class CheckVowelSwitchcase1{
public static void main(String args[]){
    char ch;//variable declaration
    boolean isVowel=false;//Declare a boolean variable

 Scanner scan=new Scanner(System.in); 
    //create a scanner object for input
    
System.out.println("Enter the character  ");
ch=scan.next().charAt(0);;// store the input from the user

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':
    isVowel=true;
}
    
    if(isVowel==true){
        System.out.println(ch+" is a vowel");
    }
    else{ if((ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' )){
        System.out.println(ch+" is a consonant");
    }
    else{
        System.out.println(ch+" is not a Alphabets");
    }
}
    
}
    
}

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

Case 1

Enter a character
a
a is a vowel

 

Case 2

Enter a character
I
I is a vowel

 

Case 3

Enter a character
v
v is a consonant

 

Case 4

Enter a character
F
F is a consonant

 

Case 5

Enter a character
$
$ is not a character

 

Approach

 

Suggested for you

Switch statements in Java

Operator in Java

Data type and variable in Java

If statements in Java

 

Similar post

C program to check Vowel or consonant using switch case statements

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

C Check whether the given alphabet is upper case or lowercase
C++ program to Check Vowel or consonant using switch case
Exit mobile version