Java Program to Check Whether an Alphabet is Vowel or Consonant
Java if…else Statement
- The if…else statement in Java is a control flow statement that allows you to execute different code blocks based on the outcome of a condition.
You can also use nested if…else statements to handle more complex conditions.
You must be familiar with following topics to understand the correspond example Such as: Java if… else statement and Java Switch Statement.
- To understand the how to Check weather an Alphabet is Vowel or Consonant, Read the Complete Article.
Steps to Check weather an Alphabet is Vowel or Consonant:
- Here are the steps to Steps to Check weather an Alphabet is Vowel or Consonant:
- Convert the given alphabet to lowercase, if it is uppercase, as Java considers both upper and lowercase characters as separate characters.
- Check if the given alphabet is one of the five vowels: ‘a’, ‘e’, ‘i’, ‘o’, ‘u’.
- If the alphabet is one of the five vowels, print “Vowel”.
- If the alphabet is not one of the five vowels, print “Consonant”.
Java Switch Statement:
The switch statement in Java is a control flow statement that is used to choose between multiple branches of code based on the value of an expression. It is an alternative to the if..else statement and can be more efficient and easier to read in certain situations where you want to match the value of an expression with a number of different cases.
The basic Syntax of Java switch Statement:
switch (expression) { case value1: // code to execute when expression equals value1 break; case value2: // code to execute when expression equals value2 break; ... default: // code to execute when expression doesn't match any of the case values break; }
Java if...else Statement:
The if...else statement in Java is a control flow statement that allows you to execute different code blocks depending on the outcome of a condition.
They were introduced in Java 8 and provide a concise way to write functional-style code.
The basic syntax for if…else statement is:
if (condition) { // statements to execute if the condition is true } else { // statements to execute if the condition is false }
Let’s look at the Java Program to Check weather an Alphabet is Vowel or Consonant to perform certain operations.
Example 1: Java Program to Check weather an Alphabet is Vowel or Consonant
Run
public class Main { public static void main(String[] args) { char ch = 'b'; if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ) System.out.println(ch + " is a vowel."); else System.out.println(ch + " is a consonant."); } }
Output
b is a consonant.
Explanation:
This is a Java program that checks whether a character is a vowel or consonant.
The character is stored in a variable ch and is initialized to the value 'b'.
The program uses an if..else statement to check if the value of ch is one of the five vowels 'a', 'e', 'i', 'o', or 'u'. If it is, the program prints "ch is a vowel."; otherwise, it prints "ch is a consonant." In this case, since ch is assigned the value 'b', the program will print "b is a consonant."
Example 2 :Java Program to Check weather an Alphabet is Vowel or Consonant
Run
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter an alphabet: "); char ch = sc.next().charAt(0); ch = Character.toLowerCase(ch); switch(ch) { case 'a': case 'e': case 'i': case 'o': case 'u': System.out.println(ch + " is a vowel."); break; default: System.out.println(ch + " is a consonant."); } } }
Output
Enter an alphabet: A a is a vowel.
Explanation:
In this code, the user is prompted to enter an alphabet, and the program converts the input to lowercase (if it is uppercase) using the Character.toLowerCase method.
Then, it uses a switch statement to match the input alphabet with the five vowels 'a', 'e', 'i', 'o', or 'u'.
If a match is found, the program prints "The alphabet is a vowel."; otherwise, it prints "The alphabet is a consonant."
Example 3: Java Program to Check weather an Alphabet is Vowel or Consonant.
Run
public class Main{ public static void main(String[] args) { char ch = 'i'; if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ) System.out.println(ch + " is vowel"); else System.out.println(ch + " is consonant"); } }
Output
i is vowel
Explanation:
This Java code defines a class Main with a main method that takes no arguments.
The main method declares a variable ch and assigns it the value 'i'.
Then, it uses an if..else statement to check if the value of ch is one of the five vowels 'a', 'e', 'i', 'o', or 'u'.
If it is, the program prints "ch is a vowel."; otherwise, it prints "ch is a consonant."
In this case, since ch is assigned the value 'i', the program will print "i is a vowel."
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment