Q. Java Program to Check Whether the Given Year is Leap Year or Not.


Here you will find an algorithm and program in C programming language to check whether the given year is leap year or not. First let us understand what is leap year?

Explanation : A leap year is a calendar year that contains an additional day added to keep the calendar year synchronized. A leap year is exactly divisible by 4, except for the century years (the years ending with 00). Century year is a leap year only if it is completely divisible by 400.

For Example : 2012 is Leap Year, 2014 is not a leap year.

Leap Year Algorithm

START
Step 1 → Initialize variable year
Step 3 → Check if year is divisible by 4 but not by 100, DISPLAY "leap year"
Step 4 → Check if year is divisible by 400, DISPLAY "leap year"
Step 5 → Otherwise, DISPLAY "not leap year"
STOP


Java Program to Check Leap Year

public class Main {
	public static void main(String[] args) {
		int check_year = 1998;
		boolean leap = false;
		if(check_year % 4 == 0)
		{
			if( check_year % 100 == 0)
			{
				// check_year is divisible by 400, hence the check_year is a leap year
				if ( check_year % 400 == 0)
					leap = true;
				else
					leap = false;
			}
			else
			leap = true;
		}
		else
			leap = false;
		
		if(leap)
			System.out.println(check_year + " is a leap year.");
		else
			System.out.println(check_year + " is not a leap year.");
	}
}

Output

1998 is not a leap year.