March 2 2015

Leap year program in java

Leap year program in java

A normal year has 365 days.
A leap year has 366 days (the extra day is the 29th of February)

example : 2012, 2016, 2020, 2024 …

Algorithm

1. Take the year from the user (year)
2. If the year is divisible by 400 it is a leap year
3. If the year is divisible by 4 and not divisible by 100 it is a leap year
4. If step 2 or step 3 conditions are not satisfied,It is a not leap year

Example

2000, 2400 are divisible by 400 . They are leap years.
2012, 2016 are divisible by 4 and not divisible by 100 .They are leap years.
2100, 2200 are divisible by 4 and 100. They are not leap years

Leap year program in java

package com.answersz.impprograms;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class LeapYearProgram {

/**
* Leap year program in java
*/
public static void main(String[] args) throws NumberFormatException,IOException {

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the year to check leap year or not :");
int year = Integer.parseInt(br.readLine());

if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) {
System.out.println(year + " is a leap year");
} else {
System.out.println(year + " is not a leap year");
}

}

}

Output

Enter the year to check leap year or not :
2012
2012 is a leap year

Enter the year to check leap year or not :
2000
2000 is a leap year

Enter the year to check leap year or not :
2100
2100 is not a leap year


Copyright © 2014. All rights reserved.

Posted March 2, 2015 by answersz in category Bluej Java Programs for ICSE Class 10 and 9 Practice, Core Java

Leave a Comment

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

*