Leap Year Program In Java

Today you will learn leap year program in java with an example. As everybody know leap year occurs every four years as opposed to common year. Now we will see its code implementation,

Leap Year Program In Java

Example: Leap Year Program In Java Without Scanner class

Also Read – Date Time Java Program

public class Leap_Year {

public static void main(String[] args) {

int year = 2007;

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

Output: Year 2000 is a leap year.


Now with Scanner class,

Example: Leap Year Program In Java With Scanner class

Also Read – Hover HTML Example

import java.util.Scanner;

public class Leap_Year {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
System.out.println("Please enter year: ");
int year = sc.nextInt();

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

Output:

Please enter year: 2004
Year 2004 is a leap year.

About the author

admin

View all posts

Leave a Reply

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