Skip to main content

Command Palette

Search for a command to run...

Java Programming : vowel or consonant

Updated
2 min readView as Markdown
Java Programming : vowel or consonant

Example

Input
a
Output
Vowel

Required to Know

Use of Variables, Use of different operators like assignment or arithmetic operators, Use of methods like System. out.print(), and use of next().charAt(0), Use of If-else, and Use of Data types.

Steps

  • Take the character as input from the user.

  • We need to check the conditions for the vowel. As we know vowel starts with a and end with u.

  • So the condition will be rather in the form of if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u'|| ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U').

  • If the character entered by the user falls under this range, print it as a vowel. Otherwise, we have to print it as a consonant.

Program

import java.util.Scanner;

public class vowel {

  /**
   * This program checks if a character is a vowel or a consonant
   */
  public static void main(String[] args) {
    // Create a Scanner object to read user input.
    Scanner sc = new Scanner(System.in);

    // Prompt the user to enter a character.
    System.out.print("Enter the character: ");
    char ch = sc.next().charAt(0);

    // Check if the character is a vowel.
    if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u'|| ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
      // The character is a vowel.
      System.out.println("It is a vowel");
    } else {
      // The character is a consonant.
      System.out.println("It is a consonant");
    }
  }
}

More from this blog

TheCodingcoder

60 posts

Empowering the coding community by simplifying complex tasks.