ADVERTISEMENT
ADVERTISEMENT

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 Solution:

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.

Java Basic Programs »


ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.