All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Leap Year Program In Java Using For Loop

Last Updated : Mar 11, 2024

Leap Year Program In Java Using For Loop

In this article we will show you the solution of leap year program in java using for loop, the leap year is also called an intercalary year or a leap year. While a regular year has 365 days, the leap year has 366 days.

There is an extra day added to compensate for the drift between a purely astronomical year and a seasonally-based one.

Around 365.25 days are required for the Earth's revolution to complete. The extra day is added every four years to bridge that additional time period.

A recent link explains why there are no leap years in 1800, 1900, and 2100, even though those years have been divisible by four values.

We will now discuss the idea of how to create a leap year program in Java using a for loop with an example.

Step By Step Guide On Leap Year Program In Java Using For Loop :-

public class TCLeapYear
{
boolean isLeapYr(int y)
{
if((y % 100) == 0)
{
    if((y % 400) == 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}
if((y % 4) == 0)
{
    return true;
}
return false;
}
public static void main(String argvs[])
{
LeapYear obj = new LeapYear();
int year = 1996;
if(obj.isLeapYr(year))
{
    System.out.println("The year " + year + " is leap year.");
}
else
{
    System.out.println("The year " + year + " is not leap year.");
}
System.out.println();
// input 2
year = 1999;
if(obj.isLeapYr(year))
{
    System.out.println("The year " + year + " is leap year.");
}
else
{
    System.out.println("The year " + year + " is not leap year.");
}
System.out.println();
// input 3
year = 1700;
if(obj.isLeapYr(year))
{
    System.out.println("The year " + year + " is leap year.");
}
else
{
    System.out.println("The year " + year + " is not leap year.");
}
System.out.println();
// input 4
year = 2000;
if(obj.isLeapYr(year))
{
    System.out.println("The year " + year + " is leap year.");
}
else
{
    System.out.println("The year " + year + " is not leap year.");
}
}
}
  1. Defining LeapYear involves creating a class.
  2. The isLeapYr method takes a parameter y that represents leap year, representing an integer.
  3. In this method, we check whether the year is a leap year, and if it is, we return true, otherwise, we return false.
  4. When the year is divisible by 100 (i.e., it is a century year), it is a leap year only if it is also divisible by 400.
  5. A leap year is only a leap year if it is divisible by four if the year is not a century year (i.e., not divisible by 100).
  6. A definition is given for the main method.
  7. In this step, a LeapYear object is created.
  8. A 1996 input year is used as the starting point.
  9. As a result, the isLeapYr method is called with the first input year as an argument, and the output is displayed as a result.
  10. With 1999 as the second input year, the data is set as follows.
  11. The isLeapYr method is called with the second input year as an argument, and the output is printed accordingly.
  12. As an input year for the third time period, 1700 is selected.
  13. As a result, the third input year is called an argument to the isLeapYr method, and the output accordingly is printed.
  14. 2000 has been set as the fourth input year.
  15. As an argument, the fourth input year is passed to the isLeapYr method, which prints the result accordingly.

Conclusion :-

As a result, we have successfully learned how to create a leap year program in Java using a for loop with an example.

Using the above description, we can clearly see what a leap year is as well as the logic that underlies it, as well as the code that implements it.

There are many programmers who get confused that leap year comes after every four years, not realizing that century years come after every hundred years.

I hope this article on leap year program in java using for loop helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Dikshita

Passionate Electronics and Communication Engineering student with expertise in web development (HTML, CSS, JS,PHP, Bootstrap, React.js) and content writing. Eager problem solver and tech enthusiast, adept at creating engaging web experiences.

Follow Dikshita On Linkedin 🡪