5 different approaches to check vowel or consonant in java cy

Here you can learn, how to check whether a alphabet is vowel or consonant with 5 different approaches. codeYourslf java for beginners.

Vowel or consonant in java :- 

5 different approaches to check vowel or consonant in java cy

- Here you can learn 5 different approaches to check vowel or consonant in java programming language.

Approach :- 1 "Check vowel or consonant using if-else statement"

// if....else statement

package asicQuestion;

import java.util.Scanner;

public class vowelOrconsonant1 {

  public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);

    System.out.println("Enter the alphabet: ");

    char ch = sc.next().charAt(0);

    ch = Character.toLowerCase(ch);

    if (ch == 'a') {

      System.out.println(ch + " is vowel.");

    } 

    

    else if (ch == 'e') {

      System.out.println(ch + " is vowel.");

    } 

    else if (ch == 'i') {

      System.out.println(ch + " is vowel.");

    } 

    else if (ch == 'o') {

      System.out.println(ch + " is vowel.");

    } 

    else if (ch == 'u') {

      System.out.println(ch + " is vowel.");

    } 

    

    else {

      System.out.println(ch + " is consonant");

    }

  }

}

    

Output

5 different approaches to check vowel or consonant in java cy

- Character.toLowerCase(ch), this will convert the capital letters to small.

- This program will first check whether the entered alphabet (ch) is equal to 'a' or not. If both matches it will print ch is vowel, If not it will move to next condition.

- now one by one it will check for e, i, o, u. If not satisfied then it will print the else part.

Approach :- 2 "using OR operator in if-else statement"

// Using OR operator with if-else

package asicQuestion;

import java.util.Scanner;

public class vowelOrconsonant2 {

    public static void main(String args[]){

    Scanner sc = new Scanner(System.in);

    System.out.println("Enter the alphabet: ");

    char ch = sc.next().charAt(0);

    ch = Character.toLowerCase(ch);

    

    if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u'){

      System.out.println(ch + " is vowel");

    }

    

    else{

      System.out.println(ch + " is consonant");

    }

    

    }

}

    

- Here all the condition are checked in a single if block with OR operator

-If one of the condition become true it will print vowel, otherwise print it is consonant.

Approach :- 3 " Vowel or consonant using switch case in java"

package asicQuestion;

import java.util.Scanner;

public class vowelOrconsonant3 {

    public static void main(String []args){

    Scanner sc = new Scanner(System.in);

    System.out.println("Enter the alphabet: ");

    char ch = sc.next().charAt(0);

    ch = Character.toLowerCase(ch);

    

    switch(ch){

      case 'a': System.out.println(ch +" is vowel"); break;

      case 'e': System.out.println(ch +" is vowel"); break;

      case 'i': System.out.println(ch +" is vowel"); break;

      case 'o': System.out.println(ch +" is vowel"); break;

      case 'u': System.out.println(ch +" is vowel"); break;

      

      default: System.out.println(ch + " is consonant");

    }

    

    }

}

    

- Here ch(entered alphabet) will go through all the cases, if it is found that alphabet mentioned in the case it will print vowel. Otherwise it will go to default option.

Approach :- 4 "Using optimised switch case"

package asicQuestion;

public class vowelOrConsonant4 {

    public static void main(String args[]){

      char ch = 'a';

      

      switch(ch){

        case'a':

        case'e':

        case'i':

        case'o':

        case'u': System.out.println(ch + " is vowel"); break;

        

        default: System.out.println(ch + " is consonant");

      }

    }

}

 

-We can write a single System.out.println(); for all the cases.

Approach :- 5 "Alphabet is vowel or consonant using method in java"

package asicQuestion;

import java.util.Scanner;

public class vowelOrconsonant5 {

    public static void main(String args[]){

    Scanner sc = new Scanner(System.in);

    System.out.println("Enter the alphabet: ");

    char ch = sc.next().charAt(0);

    ch = Character.toLowerCase(ch);

    

    //creating object 

    checkVowelConsonant vc = new checkVowelConsonant();

    //calling method using the object

    boolean result = vc.vowelOrConsonant(ch);

    

    if(result == true){

      System.out.println(ch +" is vowel");

    }

    

    else{

      System.out.println(ch + " is consonant");

    }

    }

}

class checkVowelConsonant{

  boolean vowelOrConsonant(char ch){

    if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u'){

      return true;

    }

    else{

      return false;

    }

  }

}

    

- In this program I have used java method concept to check whether a alphabet is vowel or consonant.

- I have defined another class checkVowelConsonant, inside that class I have declared a method named vowelOrConsonant.

- Now in main method I have created the object to the class checkVowelConsonant, and by using that object method have been called.

👉 Hey if you learn something new from this article, please share it with your friends.

👉 And if you have any doubt or any questions, then feel free to write it on comments. I will definitely clear your doubt.

`

2 comments

  1. Nice work ...
Hey Please 👏, feel free to share your opinion 🌍
© codeYourslf. All rights reserved. Distributed by Pixabin