Program to Check a Leap Year.

Leap Year: a year which comes after 4 years, having 366 days and in this year February has 29 days.
Now, let us look at steps how to check a year is leap year or not. 

(Following logic can be used). 

Steps:

1. If the year is representing beginning of a century like 1800,2000,3000 etc. then it should be divisible by 400. only then, it will be called as leap year. 
    To know whether a given year is century year or not then it should be divisible by 100. 
    If, both conditions satisfy then year is called a leap year.
 
2. If the year is not century year like 2001,2002,1999 etc. then it should be divisible by 4. Only then it is leap year.
 
3. If and only if both above conditions(in step 1 and step 2) satisfies then a year is called a leap year, otherwise not.   
 

Code Approach:


import java.util.Scanner;

//java program to check a year is leap or not
public class LeapYear {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a year: ");
int year = sc.nextInt();
if(year%400==0 && year%100==0)
System.out.print(year+" is a leap year.");
else if(year%4==0 && year%100!=0)
System.out.println(year+" is a leap year.");
else
System.out.print(year+" is not a leap year.");
}
}



ADVERTISEMENT



Output:

Enter a year: 2004
2004 is a leap year.

Process finished with exit code 0



Also read:
Previous Post Next Post

Contact Form