Write a program to Check whether a character is a vowel or consonant using switch statement
- import java.util.Scanner; - This line imports the Scanner class from the java.util package, which allows the user to input values from the console.
- public class Vowel_Consonant - This line starts the Vowel_Consonant class, which contains the main method for the program.
- public static void main(String[] args) - This is the main method, which is the entry point of the program.
- Scanner input = new Scanner(System.in); - This line creates a new Scanner object named input to read input from the console.
- char ch; - This declares a variable named of type char.
- System.out.printf("Enter a Character : "); - This line prints the prompt for the user to enter a character.
- ch = input.next().charAt(0); - This line reads the next character entered by the user and assigns it to the ch variable.
- if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) - This line checks if the character entered by the user is an alphabet (either uppercase or lowercase).
- switch (ch) - If the character is an alphabet, this line starts a switch statement based on the value of ch.
- case 'A': - This line starts a case for the uppercase letter 'A'.
- case 'E': - This line starts a case for the uppercase letter 'E'.
- case 'I': - This line starts a case for the uppercase letter 'I'.
- case 'O': - This line starts a case for the uppercase letter 'O'.
- case 'U': - This line starts a case for the uppercase letter 'U'.
- case 'a': - This line starts a case for the lowercase letter 'a'.
- case 'e': - This line starts a case for the lowercase letter 'e'.
- case 'i': - This line starts a case for the lowercase letter 'i'.
- case 'o': - This line starts a case for the lowercase letter 'o'.
- case 'u': - This line starts a case for the lowercase letter 'u'.
- System.out.println("This is a Vowel"); - If the character matches one of the cases above, this line prints the message "This is a Vowel".
- default: - If the character does not match any of the cases above, this line starts the default case.
- System.out.println("This is a Consonant"); - If the character is an alphabet but does not match any of the cases above, this line prints the message "This is a Consonant".
- else - If the character entered by the user is not an alphabet, this line starts the else block.
- System.out.println("This is Not an alphabet"); - This line prints the message "This is Not an alphabet".
Source Code
import java.util.Scanner;
public class Vowel_Consonant
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
char ch;
System.out.printf("Enter a Character : ");
ch = input.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.println("This is a Vowel");
break;
default:
System.out.println("This is a Consonant");
break;
}
}
else
{
System.out.println("This is Not an alphabet");
}
}
}
Output
Enter a Character : o
This is a Vowel