Wednesday, July 3, 2024
HomeLanguagesJavaJava Program to Check Whether the Character is Vowel or Consonant

Java Program to Check Whether the Character is Vowel or Consonant

For any given character, we need to check if it is a vowel or a consonant. As we know, vowels are ‘a’, ‘e’, ‘i’, ‘o’, ‘u’ and all the other characters (i.e. ‘b’, ‘c’, ‘d’, ‘f’ …..) are consonants.

Lightbox

Examples:  

Input : char = 'r'
Output : Consonant

Input : char = 'e'
Output : Vowel

Here, in the below implementation we will check if the stated character corresponds to any of the five vowels. And if it matches, “Vowel” is printed, else “Consonant” is printed.

Example 1:

Java




// java program to check whether input
// character is a vowel or consonant
  
import java.io.*;
  
public class geek {
  
    // Function to find whether an input
    // character is vowel or not
    static void Vowel_Or_Consonant(char y)
    {
        if (y == 'a' || y == 'e' || y == 'i' || y == 'o'
            || y == 'u')
            System.out.println("It is a Vowel.");
        else
            System.out.println("It is a Consonant.");
    }
  
    // The Driver code
    static public void main(String[] args)
    {
        Vowel_Or_Consonant('b');
        Vowel_Or_Consonant('u');
    }
}


Output

It is a Consonant.
It is a Vowel.

Example 2:

  • Alteration for capital letters.

Java




// java program to check whether input
// character is a vowel or consonant
  
import java.io.*;
  
public class geek {
  
    // Function to find whether an input
    // character is vowel or not
    static void Vowel_Or_Consonant(char y)
    {
        if (y == 'a' || y == 'e' || y == 'i' || y == 'o'
            || y == 'u' || y == 'A' || y == 'E' || y == 'I'
            || y == 'O' || y == 'U')
            System.out.println("It is a Vowel.");
        else
            System.out.println("It is a Consonant.");
    }
  
    // The Driver code
    static public void main(String[] args)
    {
        Vowel_Or_Consonant('W');
        Vowel_Or_Consonant('I');
    }
}


Output

It is a Consonant.
It is a Vowel.

Example 3:

Java




// java program to check whether input
// character is a vowel or consonant
  
import java.io.*;
  
class GFG {
    // Function to find whether an input
    // character is vowel or not
    static String isVowel(char ch)
    {
        // Make the list of vowels
        String str = "aeiouAEIOU";
        return (str.indexOf(ch) != -1) ? "Vowel"
                                       : "Consonant";
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        System.out.println("It is a " + isVowel('a'));
        System.out.println("It is a " + isVowel('x'));
    }
}


Output

It is a Vowel
It is a Consonant

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments