Alphabets

Java program to check whether an Alphabet is Vowel or Consonant|4 ways

Java program to check whether an Alphabet is Vowel or Consonant

In this article, we will discuss the concept of Java program to check whether an Alphabet is Vowel or Consonant

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

Code to check given Alphabet is Vowel or consonant

Code to check given Alphabet Vowel or consonant, using If else

In this code, we are going to learn how to check  the given  English alphabet is Vowel or consonant using if-else in Java language

Program 1

import java.util.Scanner;
class Check_Alpha_Vowel1{//class declaration
  
public static void main(String args[]){
char ch;//Declare a local character variable
Scanner scan=new Scanner(System.in);
//Ask input from the user
System.out.print("Enter an Alphabet: ");
//get input and store ch variable
ch=scan.next().charAt(0);
if(ch=='a'||ch=='A'||ch=='e'||ch=='E'||ch=='i'||ch=='I'||ch=='o'||ch=='O'||ch=='u'||ch=='U')
{//check the given Alphabet is vowel or consonant
  System.out.print(ch+" is a vowel");
}
else{
  System.out.print(ch+" is a consonant");
}
}
}

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

Case 1

Enter an Alphabet:
E
E is a Vowel

Case 2

Enter an Alphabet:
r
r is a Consonant

 

Code to check given Alphabet Vowel or consonant using If else-if else

In this code, we are going to learn how to check  the given  English alphabet is Vowel or consonant using if-else-if else in Java language

Program 2

import java.util.Scanner;
class Check_Alpha_Vowel2{//class declaration
  
public static void main(String args[]){
char ch;//Declare character (local) variable
Scanner scan=new Scanner(System.in);
//This statements takes input from the user
System.out.print("Enter a character:(Alphabet or Not) ");
//get input and it is stored ch variable
ch=scan.next().charAt(0);
if(ch=='a'||ch=='A'||ch=='e'||ch=='E'||ch=='i'||ch=='I'||ch=='o'||ch=='O'||ch=='u'||ch=='U')
{//this statements checks vowels
  System.out.print(ch+" is a vowel");
}
else if//"else if" statement checks consonant
((ch>='A' && ch<='Z')||(ch>='a' && ch<='z')){
  System.out.print(ch+" is a consonant");
}
else{
System.out.print(ch+" is not an Alphabet");
}//print non-alphabet characters
}
}

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

Case 1

Enter a character:(Alphabet or Not)
U
U is a Vowel

Case 2

Enter a character:(Alphabet or Not)
t
t is a Consonant

Case 3

Enter a character:(Alphabet or Not)
5
5 is not an Alphabet


Code to check given Alphabet Vowel or consonant using Nested-If

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

Program 3

import java.util.Scanner;
class Check_Alpha_Vowel3{//class declaration
  
public static void main(String args[]){
char ch;//local variable
Scanner scan=new Scanner(System.in);
//This statements takes input from the user
System.out.print("Enter a character: ");
//get input and it is stored ch variable
ch=scan.next().charAt(0);
if
((ch>='A' && ch<='Z')||(ch>='a' && ch<='z')){ //outer if use to check consonant
  
  if(ch=='a'||ch=='A'||ch=='e'||ch=='E'||ch=='i'||ch=='I'||ch=='o'||ch=='O'||ch=='u'||ch=='U')
{//inner if statements checks vowels
  System.out.print(ch+" is a vowel");
}
else{//display consonant
System.out.print(ch+" is a consonant");
}

}
else{
System.out.print(ch+" is not an Alphabet");
}

}
}

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

Case 1

Enter a Alphabet: F
F is a Consonant

Case 2

Enter a Alphabet: I
I is a Vowel

Case 3

Enter a Alphabet: &
& is not an Alphabet

 

Code to check given Alphabet Vowel or not using switch case

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 4

import java.util.Scanner;
public class CheckVowConsSwitchcase1{
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 in ch variable from the user

switch(ch){
  //check lower case vowel letters
  case 'a':
  System.out.println(ch+" is an vowel");
  break;
  
  case 'e':
  System.out.println(ch+" is an vowel");
  break;
  
  case 'i':
  System.out.println(ch+" is an vowel");
  break;
  
  case 'o':
  System.out.println(ch+" is an vowel");
  break;
  
  case 'u':
  System.out.println(ch+" is an vowel");
  break;
  
  //check upper case vowel letters
  case 'A':
  System.out.println(ch+" is an vowel");
  break;
  
  case 'E':
  System.out.println(ch+" is an vowel");
  break;
  
  case 'I':
  System.out.println(ch+" is an vowel");
  break;
  
  case 'O':
  System.out.println(ch+" is an 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
E
E is a vowel

 

Case 2

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

 

 

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

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.