Java Leap Year

  1. Check given year is Leap year or not in Java
  2. Find Leap years between two years in Java
  3. Calculate total number of Leap year between two years in Java

Note
Here I am using,
OS : Linux (Ubuntu 12.04)
IDE : Eclipse Tool
Eclipse : Juno (Version 4.2.0)
Package : Default package

A. Check given year is Leap year or not in Java


Leap_Ex1.java

import java.util.Scanner;
 
public class Leap_Ex1 {
 
	int year;
	Scanner scan;
	
	void check() {
		
		System.out.println("Check leap year or not");
 
		System.out.println("\nEnter the year : ");
		scan = new Scanner(System.in);
		
		year = Integer.parseInt(scan.nextLine());
		
		if(year%400 == 0 || year%4 == 0) 
			System.out.println(year + " is a leap year");

		else 
			System.out.println(year + " is not a leap year");
	}
}
 
class MainClass {
	
	public static void main(String str[]) {
		
		Leap_Ex1 obj = new Leap_Ex1();
		
		obj.check();
	}
}

Sample Output

Check leap year or not

Enter the year : 
1994
1994 is not a leap year
Check leap year or not

Enter the year : 
2012
2012 is a leap year




B. Find Leap years between two years in Java


Leap_Ex2.java

import java.util.Scanner;
 
public class Leap_Ex2 {
 
	int year1, year2;
	Scanner scan;
	
	void getVal() {
		
		scan = new Scanner(System.in);
 
		System.out.println("Find leap years between two years");
		
		System.out.println("\nEnter the year - 1 : ");
		year1 = Integer.parseInt(scan.nextLine());
		
		System.out.println("Enter the year - 2 : ");
		year2 = Integer.parseInt(scan.nextLine());
	}
	
	void check() {

		System.out.println();
						
		for(int i=year1; i<=year2; i++) {
			
			if(i%400 == 0 || i%4 == 0) {
				
				System.out.println(i + " is a leap year");			
			}
		}
	}
}
 
class MainClass {
	
	public static void main(String str[]) {
		
		Leap_Ex2 obj = new Leap_Ex2();
		
		obj.getVal();
		obj.check();
	}
}

Sample Output

Find leap years between two years

Enter the year - 1 : 
1990
Enter the year - 2 : 
2012

1992 is a leap year
1996 is a leap year
2000 is a leap year
2004 is a leap year
2008 is a leap year
2012 is a leap year




C. Calculate total number of Leap year between two years in Java


Leap_Ex3.java

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
 
public class Leap_Ex3 {
 
	int year1, year2;
	Scanner scan;
	List<Integer> list;
	
	void getVal() {
		
		scan = new Scanner(System.in);
 
		System.out.println("Find number of leap years between two years");
		
		System.out.println("\nEnter the year - 1 : ");
		year1 = Integer.parseInt(scan.nextLine());
		
		System.out.println("Enter the year - 2 : ");
		year2 = Integer.parseInt(scan.nextLine());
	}
	
	void check() {
		
		list = new ArrayList<Integer> ();
				
		for(int i=year1; i<=year2; i++) {
			
			if(i%400 == 0 || i%4 == 0) {
				
				list.add(i);			
			}
		}
		
		System.out.println("\nThe Total number of year is : " + list.size());
	}
}
 
class MainClass {
	
	public static void main(String str[]) {
		
		Leap_Ex3 obj = new Leap_Ex3();
		
		obj.getVal();
		obj.check();
	}
}

Sample Output

Find number of leap years between two years

Enter the year - 1 : 
1990
Enter the year - 2 : 
2012

The Total number of year is : 6






SHARE THIS PAGE



product 2

product 3

Feedbacks : balaji.scz@gmail.com