Alphabet

Program to Check whether an Alphabet is vowel or consonant in Java

Program to Check whether an Alphabet is a vowel or consonant in Java

In this article, we will discuss the concept of Program to Check whether an Alphabet is a vowel or consonant in Java programming language

In the English language, ‘a’, ‘e’, ‘i’, ‘o’, ‘u’ are vowels and remaining letters are consonant

In this post, we are going to learn how to find whether and the given alphabet is vowel or consonant

Check vowel or consonant

Program to find an Alphabet is a vowel or consonant

Check whether an Alphabet is a vowel or consonant in Java using if-else

Program 1

The program allows the user to enter an Alphabet thereafter it will  check and display the result of the given character whether “it is a vowel” or “consonant” using the if-else statements in Java language

import java.util.Scanner;
class Check_Vowel{//class declaration
    
public static void main(String args[]){//main metod declaration
char ch;//local variable
Scanner scan=new Scanner(System.in);
//This statements takes input from theuser
System.out.print("Enter an Alphabet: ");
//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')
{
    System.out.print(ch+" is vowel");
}
else{
    System.out.print(ch+" is  consonant");
}
}//end of mainmethod
}

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

case 1

Enter an Alphabet: a
a is vowel

 

Case 2

Enter an Alphabet: E
E is vowel

 

Case 3

Enter an Alphabet: M
M is consonant

 

Case 4

Enter an Alphabet: b
b is consonant

 

Approach

  • In the program, the user is asked to enter an Alphabet and entered Alphabet is stored in character variable ch
  • The program evaluates whether the entered Alphabet is a vowel or consonant, using if-else statements
  • If the given Alphabet is a vowel The program displays the output “it is a vowel” and if the given Alphabet is not vowel it will display”it is consonant”

 

Check whether an Alphabet is a vowel or consonant in Java using if-else if-else

The program allows the user to enter an Alphabet thereafter it will  check and display the result of the given character whether it is a vowel or consonant using the if-else-if else statements in Java language

Program 2

import java.util.Scanner;
class Check_Vowel2{//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 an Alphabet: ");
//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')
{//if statements checks vowels
    System.out.print(ch+" is vowel");
}
else if//else if statement checks consonant
((ch>='A' && ch<='Z')||(ch>='a' && ch<='z')){
    System.out.print(ch+" is  consonant");
}
else{
System.out.print(ch+" is not an alpabet");
}
}
}

 

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

Case 1

Enter an Alphabet: A
A is vowel

 

Case 2

Enter an Alphabet: e
e is vowel

 

Case 3

Enter an Alphabet: H
H is consonant

 

Case 4

Enter an Alphabet: m
m is consonant

 

Case 4

Enter an Alphabet: %
% is not an alphabet

 

Approach

  • In the program, the user is asked to enter an Alphabet and entered Alphabet is stored in character variable ch
  • The program evaluates whether the entered Alphabet is a vowel or consonant, using if else-if else statements
  • If the given Alphabet is a vowel The program displays the output “it is a vowel” and if the given Alphabet is not vowel it will display”it is consonant”

 

Check whether an Alphabet is a vowel or consonant in Java using Nested if-else statements

The program allows the user to enter an Alphabet thereafter it will  check and display the result of the given character whether it is a vowel or consonant using the Nested if statements in Java language

Program 3

import java.util.Scanner;
class Check_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 an Alphabet: ");
//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 vowel");
}
else{//display consonant
System.out.print(ch+" is consonant");
}

}
else{
System.out.print(ch+" is neither a vowel nor a consonant");
}

}
}

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

Case 1

Enter an Alphabet: A
A is vowel

 

Case 2

Enter an Alphabet:e
e is vowel


Case 3

Enter an Alphabet : G
G is consonant

 

Case 4

Enter an Alphabet: f
f is consonant


Case 5

Enter an Alphabet:&
& is neither a vowel nor a consonant

 

Approach

  • In the program, the user is asked to enter an Alphabet and entered Alphabet is stored in character variable ch
  • The program evaluates whether the entered Alphabet is a vowel or consonant, using nested if statements
  • If the given Alphabet is a vowel The program displays the output “it is a vowel” and if the given Alphabet is not vowel it will display”it is consonant”

Suggested for you

The operator in Java language

Data type and variable in Java

If-else statements in Java Language

Class and main method in Java

 

Similar post

C code to check whether the Alphabet is vowel or consonant

C++ code to check whether the Alphabet is vowel or consonant

Python  code to check whether the Alphabet is vowel or consonant

 

 

C code to check whether the character is Alphabet or not

C++ code to check whether the character is Alphabet or not

Java code to check whether the character is Alphabet or not

Python code to check whether the character is Alphabet or not

 

 

Python programming code to check whether the character is Alphabet or not
Program to Check whether an Alphabet is vowel or consonant in C++
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago