Q:

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

belongs to collection: Java Basic Programs

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

Program/Source Code:

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.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java Basic Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java program to check whether the number is EVEN o... >>
<< Java program to read gender (M/F) and print the co...