# Thread: How to write a program to determine if year is leap year or not

1. Member
Join Date
Nov 2009
Posts
50

## How to write a program to determine if year is leap year or not

Hi,
I am last year B.Sc.IT student. I just started learning java language. In our last tutorial our sir has given us one program like write a program to determine if year is leap year or not. I tried various method but I unable to write that program. Please if you know any logic behind it then give it to me.
thank you.
Last edited by Luis-Fernando; 30-11-2009 at 05:06 PM.

2. Member
Join Date
Apr 2008
Posts
1,948

## Re: How to write a program to determine if year is leap year or not

Here is the program.

Code:
import java.util.*;
import java.io.*;

public class LeapYear{
public static void main(String[] args) throws IOException{
System.out.print("Enter year : ");
int y;
try{
if(y < 1900 || y > 2100){
System.out.print("enter year that is less than 2101 and greater than 1899.");
System.exit(0);
}
GregorianCalendar c = new GregorianCalendar();
if(c.isLeapYear(y))
System.out.print("Enter year is leap year.");
else
System.out.print("Enter year is not leap year.");
}
catch(NumberFormatException ne){
System.out.print(ne.getMessage() + " is not a proper number.");
System.exit(0);
}
}
}

3. Member
Join Date
May 2008
Posts
2,012

## Re: How to write a program to determine if year is leap year or not

I think this is one of the most simplest progran I have written.

Code:
public class LeapYear {

public static void main(String[] args) {

int y = 2004;

if(y % 4 == 0)

System.out.println("Year " + y + " is Leap year");

else

System.out.println("Year " +  + " is not Leap year");
}
}

4. Member
Join Date
May 2008
Posts
2,297

## Re: How to write a program to determine if year is leap year or not

Use following code to write ptogram.

Code:
GregorianCalendar cal1 = new GregorianCalendar();
boolean opt = cal1.isLeapYear(1998);
opt = cal1.isLeapYear(2000);
opt = cal1.isLeapYear(0);

5. Member
Join Date
Oct 2005
Posts
2,393

## Re: How to write a program to determine if year is leap year or not

Code:
import java.text.ParseException;

public class MainClass {

public static void main(String[] args) throws ParseException {
System.out.println(isLeapYear(2000));
}

public static boolean isLeapYear(int y) {

if (y < 0) {
return false;
}

if (y % 400 == 0) {
return true;
} else if (y % 100 == 0) {
return false;
} else if (y % 4 == 0) {
return true;
} else {
return false;
}
}