In this program, you'll learn to check whether an alphabet is a vowel or a consonant using if..else and switch statement in Java.
Check whether an alphabet is a vowel or consonant using if..else statement
package com.javaguides.java.tutorial;
/**
 * Java Program to Check Whether an Alphabet is Vowel or Consonant
 * 
 * @author https://www.sourcecodeexamples.net/
 *
 */
public class JavaProgram {
    public static void main(String[] args) {
        char character = 'a';
        if (character == 'a' || character == 'e' || character == 'i' || character == 'o' || character == 'u') {
            System.out.println(character + " is vowel");
        } else {
            System.out.println(character + " is consonant");
        }
    }
}
Output:
a is vowelCheck whether an alphabet is a vowel or consonant using a switch statement
package com.javaguides.java.tutorial;
import java.util.Scanner;
/**
 * Java Program to check Vowel or Consonant using Switch Case
 * 
 * @author https://www.sourcecodeexamples.net/
 *
 */
public class JavaProgram {
    public static void main(String[] args) {
        try (Scanner scanner = new Scanner(System.in)) {
            boolean isVowel = false;
            System.out.println("Enter a character : ");
            char ch = scanner.next().charAt(0);
            scanner.close();
            switch (ch) {
                case 'a':
                case 'e':
                case 'i':
                case 'o':
                case 'u':
                case 'A':
                case 'E':
                case 'I':
                case 'O':
                case 'U':
                    isVowel = true;
            }
            if (isVowel == true) {
                System.out.println(ch + " is  a Vowel");
            } else {
                if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
                    System.out.println(ch + " is a Consonant");
                } else {
                    System.out.println("Input is not an alphabet");
                }
            }
        }
    }
}
Output:
Enter a character : 
J
J is a ConsonantRelated Java Programs
- Java program to calculate the area of Triangle
- Java Program to Calculate Area of Square
- Java Program to Calculate Area of Rectangle
- Java Program to find the Smallest of three numbers using Ternary Operator
- Java Program to Find Largest of Three Numbers
- Java Program to Find GCD of Two Numbers
- Java Program to Check Armstrong Number
- Java Program to Generate Random Number
- Java Program to Check if Number is Positive or Negative
- Java program to check prime number
- Java Program to Calculate Simple Interest
- Java Program to Swap Two Numbers Without using a Temporary Variable
- Java Program to Swap Two Numbers
- Java Program to Find ASCII Value of a Character
- Java Program to Check Whether an Alphabet is Vowel or Consonant
- Java Program to Check Leap Year
- Java Program to Multiply Two Numbers
- Java Program to Check Even or Odd Number
- Java Program to Add Two Numbers
- Java Program to Swap Two Strings Without Using Third Variable
- Java Program to Swap Two Strings with Third Variable
- How to Get All Digits from String in Java
- Find Duplicate Number in Array in Java
- How to Get Current Working Directory in Java?
- Check Palindrome String in Java
- Java Program to Create Pyramid Of Numbers
Comments
Post a Comment