EduLearn - Online Education Platform

Welcome to EduLearn!

Start learning today with our wide range of courses taught by industry experts. Gain new skills, advance your career, or explore new interests.

Browse Courses

Here is a simple Java program to check whether a given letter is a vowel or a consonant.


βœ… Java Code:

import java.util.Scanner;

public class VowelOrConsonant {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

// Input a character
System.out.print(“Enter a letter: “);
char ch = sc.next().charAt(0);

// Convert to lowercase for simplicity
ch = Character.toLowerCase(ch);

// Check if it’s an alphabet
if ((ch >= ‘a’ && ch <= ‘z’)) {
if (ch == ‘a’ || ch == ‘e’ || ch == ‘i’ || ch == ‘o’ || ch == ‘u’) {
System.out.println(“The letter is a vowel.”);
} else {
System.out.println(“The letter is a consonant.”);
}
} else {
System.out.println(“Invalid input. Please enter an alphabet letter.”);
}

sc.close();
}
}


πŸ“Œ Example Output:

Enter a letter: I
The letter is a vowel.

βœ… Explanation:

import java.io.*;
import java.util.*;
class Cal
{
public static void main(String[] args)
{
char c;
System.out.println(“Enter the character”);
Scanner s=new Scanner(System.in);
c=s.next().charAt(0);

switch(c)
{
case ‘a’:
case ‘e’:
case ‘i’:
case ‘o’:
case ‘u’:
case ‘A’:
case ‘E’:
case ‘I’:
case ‘O’:
case ‘U’:

System.out.println(c +”is a vowel”);
break;
default:
System.out.println(c +”is a Consonant”);
}
}
}

  • Converts the input letter to lowercase using Character.toLowerCase().

  • Checks whether the character is a valid English alphabet.

  • Then determines if it is a vowel (a, e, i, o, u) or a consonant.

Using java check whether a given letter is a vowel or a consonant.

Leave a Reply

Your email address will not be published. Required fields are marked *

EduLearn - Online Education Platform

Welcome to EduLearn!

Start learning today with our wide range of courses taught by industry experts. Gain new skills, advance your career, or explore new interests.

Browse Courses

Popular Courses

[Course Image]

Introduction to Programming

Learn the fundamentals of programming with Python in this beginner-friendly course.

12 Hours Beginner
[Course Image]

Data Science Essentials

Master the basics of data analysis, visualization, and machine learning.

20 Hours Intermediate
[Course Image]

Web Development Bootcamp

Build modern websites with HTML, CSS, JavaScript and popular frameworks.

30 Hours Beginner
[Course Image]

Digital Marketing Fundamentals

Learn SEO, social media marketing, email campaigns and analytics.

15 Hours Beginner
Educational Website Footer