×

Java Programs

Java Practice

Java program to check whether a character is a VOWEL or CONSONANT using switch statement

Given/input a character, we have to check whether a character is a VOWEL or CONSONANT using switch statement.
Submitted by Nidhi, on March 03, 2022

Problem statement

In this program, we will read a character from the user and check given character is VOWEL or CONSONANT using a switch statement.

Java program to check whether a character is a VOWEL or CONSONANT using switch statement

The source code to check whether a character is a VOWEL or CONSONANT using a switch statement is given below. The given program is compiled and executed successfully.

// Java program to check whether a character is a 
// VOWEL or CONSONANT using switch statement

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner SN = new Scanner(System.in);

    char ch;

    System.out.printf("Enter a character: ");
    ch = SN.next().charAt(0);

    if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) {
      switch (ch) {
      case 'A':
      case 'E':
      case 'I':
      case 'O':
      case 'U':
      case 'a':
      case 'e':
      case 'i':
      case 'o':
      case 'u':
        System.out.printf("%c is a VOWEL.\n", ch);
        break;

      default:
        System.out.printf("%c is a CONSONANT.\n", ch);
      }
    } else {
      System.out.printf("%c is not an alphabet.\n", ch);
    }
  }
}

Output

Enter a character: g
g is a CONSONANT.

Explanation

In the above program, we imported the "java.util.Scanner" package to read input from the user. And, created a public class Main. It contains a static method main().

The main() method is an entry point for the program. Here, we read a character from the user using the Scanner class. Then we checked input character is VOWEL or CONSONANT. After that, we printed the appropriate message.

Java Basic Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.