Java Program to find a Leap Year

by | Jan 8, 2023 | Java, Java Programs

Home » Java » Java Programs » Java Program to find a Leap Year

Introduction

This article demonstrates how you can check if the entered year is a leap year or not with the help of a Java program. Although the problem looks quite easy at first, the computation is a bit tricky. We can easily assume that all leap years are multiples of 4. That is, if a particular year is divisible by 4 with no remainder, then the year is a leap year. However, this is a naïve implementation of the leap year program. A year can also be a leap year if the following conditions are met:

  • A year is a leap year if it gets divisible by 100.
  • If a particular year is divisible by 100, then it also must be divisible by 400.
  • Other than the two conditions given above, if any other year is divisible by 4, that year is also surely a leap year.

Algorithm

  1. Start
  2. Initialize an integer variable called year. This variable will decide whether a year is a leap year or not.
  3. If the condition checks whether the variable is divisible by 4 but not 100. If the condition is true, ‘leap year’ is printed out to the console.
  4. If the year gets divisible by 400, then ‘leap year’ is again printed out to the console.
  5. For all other cases except the ones defined above, print ‘not a leap year’.

Sample Code to implement the Leap Year Problem

import java.util.Scanner;

public class Demo

{

public static void main ( String args [] )

{

int year;

System.out.println (“Enter a Year :: ”);

Scanner sc = new Scanner (System.in);

year = sc.nextInt ();

if ( ( ( year % 4 == 0 ) && ( year % 100! = 0 ) ) || ( year % 400 == 0 ) )

System.out.pintln ( “ The specified year is a leap year ” );

else

System.out.println ( “The specified year is not a leap year ” );

}

}

 

OUTPUT 1:

Enter a Year:: 2020 

The specified year is a leap year

OUTPUT 2:

Enter a Year:: 2017 

The specified Year is not a leap year

 

Author

0 Comments

Submit a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Author