Java program to check character is vowel or consonant

Java program to check character is vowel or consonant

 1. Java program to check character is vowel or consonant using if- else if statement:

Program:

import java.io.*;
import java.util.*;
class Vovels
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter any Character");
char xy=sc.next( ).charAt(0);
{
if(xy=='a'||xy=='e'||xy=='i'||xy=='o'||xy=='u'||xy=='A'||xy=='E'||xy=='I'||xy=='O'|| 
xy=='U')
{
System.out.println(xy+" is Vowel"); 
}
else if((xy>='a'&&xy<='z')||(xy>='A'&&xy<='Z'))
{
System.out.println(xy+" is Consonant"); }
}
}
}

Output:

Enter any character
x
x is consonant
Enter any character
a
a is vowel

 2. Java program to check character is vowel or consonant using Buffered Reader class:

Program:

import java.util.*;
import java.lang.*;
import java.io.*;
class EqualsToNumber
{
public static void main(String args[]) throws Exception
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the year");
int yr= Integer.parseInt(br.readLine());
if((yr%400==0)||(yr%100!=0 && yr%4==0))
{
System.out.println(yr+ " is a leap year");
}
else
{
System.out.println(yr+" is not a leap year");
}
}
}

Output:

Enter the year
2001
2001 is not a leap year

 3. Java program to check character is vowel or consonant using command line Argument:

program:

import java.util.*;
class LeapYear
{
public static void main(String args[])
{

int yr= Integer.parseInt(args[0]);
System.out.println(yr);
if((yr%400==0)||(yr%100!=0 && yr%4==0))
{
System.out.println(yr+ " is a leap year");
}
else
{
System.out.println(yr+" is not a leap year");
}
}
}

Compile:

javac LeapYear.java

Run:

java LeapYear 2001
2001 is not a leap year

 4. Java program to check character is vowel or consonant using Constructor:

Program:

import java.util.*;
class LeapYear
{
	
	LeapYear(int yr)
	{
	 if((yr%400==0)||(yr%100!=0 && yr%4==0))
{
System.out.println(yr+ " is a leap year");
}
else
{
System.out.println(yr+" is not a leap year");
}
 
	}
}
class LeapYearOrNot
{
   public static void main(String args[]) 
    {   
      Scanner sc= new Scanner(System.in);
      System.out.println("Enter the year");
      int year= sc.nextInt();      
      LeapYear obj =new LeapYear(year);
	}  
 }

Output:

Enter the year
2007
2007 is not a leap year

 

Leave a Comment