0

Here is my requirements. Determine If a Year Is a Leap Year Algorithm:

  • If the year can be evenly divided by 4, then it is a leap year
    • Except when the year can be evenly divided by 100, then it is not a leap year
  • Except when the year can be evenly divided by 400, then it is a leap year
  • Otherwise, it is not a leap year

I need to know if i did right?

private static boolean isLeapYear(int userInput){
    boolean leapYear= false;

    if (userInput % 4 == 0 ){
        leapYear = true;

        if (userInput % 4 == 0 && userInput % 100 ==0) {
            leapYear = false;

            if(userInput % 400 == 0){
                leapYear = true;
            }
        }
    }
    else {
        leapYear = false;
    }

    return leapYear;
}
jww
  • 97,681
  • 90
  • 411
  • 885
SamR
  • 517
  • 3
  • 10
  • 24

9 Answers9

0

I used this one in C++.

return ((userInput % 400) || ((userInput % 4) && !(userInput % 100)));

  • It is more efficient to put the test for 400 last. See http://stackoverflow.com/a/11595914/3466415 – David K Mar 25 '15 at 21:10
0

Better use this condition for a valid leap year
(((year%4 == 0) && (year%100 !=0)) || (year%400==0))

Here is a similar C program to check leap year.

Pankaj Prakash
  • 2,300
  • 30
  • 31
0

Correct! Simplified:

  1. Remove year % 4 from 2nd if bc already tested in 1st if
  2. Remove else bc already set leap = False at top

Python:

def is_leap(year):
    leap = False

    if year % 4 == 0:
        leap = True

        if year % 100 == 0:
            leap = False

            if year % 400 == 0:
                leap = True

    return leap

1-Line:

def is_leap(year):
    return year % 4 == 0 and year % 100 != 0 or year % 400 == 0
JBallin
  • 8,481
  • 4
  • 46
  • 51
0

I used this short method:

private static Boolean isLeapYear(int year) {
        return year % 4 == 0 ? (year % 100 == 0 ? ( year % 400 == 0 ? true : false) : true) : false ;
    }
Raf
  • 9,681
  • 1
  • 29
  • 41
Ravindra Kumar
  • 1,842
  • 14
  • 23
0
year = int(input("Enter year to determine if it is a leap year"))

def leap_year(year):
    """This is a function to determine if a year
       is a leap year"""
    if year%4==0 and year%100!=0:
        print("This is a Leap year")
        if year%400==0:
            print ("This is a Leap year")
    else:
        print ("This is not a leap year")    


leap_year(year)
HansHirse
  • 18,010
  • 10
  • 38
  • 67
  • 2
    While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – adiga Apr 05 '19 at 07:34
0

From 1700 to 1917, official calendar was the Julian calendar. Since then they we use the Gregorian calendar system. The transition from the Julian to Gregorian calendar system occurred in 1918, when the next day after January 31st was February 14th. This means that 32nd day in 1918, was the February 14th.

In both calendar systems, February is the only month with a variable amount of days, it has 29 days during a leap year, and 28 days during all other years. In the Julian calendar, leap years are divisible by 4 while in the Gregorian calendar, leap years are either of the following:

Divisible by 400.

Divisible by 4 and not divisible by 100.

So the program for leap year will be:

def leap_notleap(year):

    yr = ''
    if year <= 1917:
        if year % 4 == 0:
            yr = 'leap'
        else:
            yr = 'not leap'
    elif year >= 1919:
        if (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0):
            yr = 'leap'
        else:
            yr = 'not leap'
    else:
        yr = 'none actually, since feb had only 14 days'

    return yr
Sanket Mathur
  • 44
  • 2
  • 7
0

With python and another languages, you can use the property that if you substract 1 day to march, you have the last day of february. If is leap year, that day is 29.

from datetime import datetime, timedelta

def is_leap_year(year: int):
    marzo = datetime(year, 3, 1)
    febrero = marzo - timedelta(1)
    if febrero.day == 29:
        return True
    else:
        return False
0

Every year that is exactly divisible by four is a leap year, except for years that are exactly divisible by 100, but these centurial years are leap years if they are exactly divisible by 400. For example, the years 1700, 1800, and 1900 are not leap years, but the years 1600 and 2000 are.[2]

— United States Naval Observatory

Source: https://en.wikipedia.org/wiki/Gregorian_calendar

Thus, for a year to qualify as a leap year, it should be:

  1. Either divisible by 400 -OR-
  2. Divisible for 4 but not by 100

Use this fact in your code as follows:

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

For production code, I recommend you use the OOTB java.time API:

if (java.time.Year.isLeap(userInput)) {
    System.out.println(userInput + " is a leap year");
}

Learn more about the modern date-time API from Trail: Date Time.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
-1

userInput % 4 == 0 && userInput % 100 ==0 is equivalent to userInput % 400 == 0

and userInput % 4 == 0 then it is definitely Leap year so need not to check any other condition.

Tribhuwan
  • 180
  • 1
  • 1
  • 11