Alphabets

Java program to find whether given Alphabet is vowel or consonant using switch case

Java program to find whether given Alphabet is vowel or consonant using switch case

In this article, we will discuss the concept of Java program to find whether given Alphabet is vowel or consonant using switch case

In this post, we are going to learn how to write a program to  check whether given English alphabet is Vowel or consonant  using switch  case in Java language

Code to check given Alphabet is Vowel or consonant

Code to check given Alphabet Vowel or consonant, using switch   – #1

In this code, we are going to learn how to check  the given  English alphabet is Vowel or consonant using Switch case statements in Java language

Program 1

import java.util.Scanner;
public class CheckVowConsSwitch_case{
public static void main(String args[]){
  char ch;//character 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  ");
//Ask input from the user
ch=scan.next().charAt(0);;// store the input in ch variable

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://print if the character is constant
  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
S
S is a consonant

Case 2

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

 

Code to check given Alphabet Vowel or consonant, using switch- #2

In this code, we are going to learn how to check  the given  English alphabet is Vowel or consonant using Switch case statements in Java language

Program 2

import java.util.Scanner;
public class CheckVowelConsSwitchcase1{
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 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':
  case 'e':
  case 'i':
  case 'o':
  case 'u':
  
  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 the character for check vowel or consonant
F
F is a consonant

Case 2

Enter the character for check vowel or consonant
o
o is a Vowel

 

Suggested post

if statement in Java language

Nested if statements in Java language

Method in Java language

For loop in Java language

While loop in Java language

Do-while loop in Java language

Operator in Java language

Variable in Java language

Data type in Java language

class and main method in Java

 

Similar post

Java program to print all alphabets in given range

C program to print all alphabets in given range

C++ program to print all alphabets in given range

 

Java program to print all alphabets using loops

C program to print all alphabets using loops

C++ program to print all alphabets using loops

 

Java program to check whether a character is Alphabet or Not

C++ program to check whether a character is Alphabet or Not

C program to check whether a character is Alphabet or Not

Python program to check whether a character is Alphabet or Not

Karmehavannan

Recent Posts

Multiply two numbers in Java using scanner| 5 different ways

Multiply two numbers in Java using scanner| 5 different ways In this article, we will…

3 months ago

5 different ways to Divide two numbers in Java using scanner

5 Different ways to Divide two numbers in Java using scanner In this article, we…

3 months ago

Learn 8 Ways to Subtract Two Numbers Using Methods in Java

Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…

4 months ago

10 ways to subtract two numbers in Java

10 ways to subtract two numbers in Java In this article, we will discuss the…

4 months ago

Java Code Examples – Multiply Two Numbers in 5 Easy Ways

Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…

4 months ago

How to Divide two numbers in Java| 5 different ways

How to Divide two numbers in Java| 5 different ways In this article, we will…

4 months ago

This website uses cookies.