Home > Enterprise >  Understanding formula for leap years
Understanding formula for leap years

Time:01-18

I'm trying to understand this solution right here: What I don't understand is what means == 0 ??? And why it is: if it equals to 0?.

This was my task:

In leap years, February has 29 days instead of the usual 28 days. We can determine leap years by the fact that the year number is divisible by 4. However, the year must not be divisible by 100. If the year is divisible by 400, it is a leap year. Write a program in Java that checks whether a given year is a leap year. If the year is a leap year, true is output. If this is not the case, false is output.

I tried many things and then I wanted to look up from the solution and now I want to understand it.

import java.util.Scanner;

public class Leapyear {

    public static void main(String[] args) {

        Scanner TB = new Scanner(System.in);

        System.out.println("Type in a year which you want to test if it is a leapyear or not:");
        int year = TB.nextInt();

        boolean isLeapYear = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;

        System.out.println(isLeapYear);

    }
}

CodePudding user response:

You can split your formula into several parts to better understand it:

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

This creates a boolean variable from the year, boolean can only be true or false, while year is of type int, a whole number with a range enough for years (the exact values don't matter for this answer).

First you can set more parenthesis to make it clearer and separated:

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

Then the parts explained:

  • (year % 4) calculates the remainder of division by 4
  • ((year % 4) == 0) is true, if the remainder of the division by 4 is 0, that means, it is dividable by 4 without remainder => dividableBy4
  • ((year % 100) != 0)) similar to above (is true if year is not dividable by 100 without remainder) => notDividableBy100
  • ((year % 400) == 0)) the same as with 4 but for division by 400 => dividableBy400

Now you have:

(dividableBy4 && notDividableBy100) || dividableBy400

&& and || are operators for boolean variables, && stands for logical AND, || is for logical OR, so the expression above means:

either (dividable by 4 AND not dividable by 100) or dividable by 400

CodePudding user response:

The "TB" is a Input scanner that gets the input you put in your Console.

int year = TB.nextInt();

Converts your String input into an int

boolean isLeapYear = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;

you could also wirte it like this

if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0){
    isLeapYear = true;
}else{isLeapYear = false;}

% opperator takes makes a division and returns the remainder for example 9 % 4 = 1, because 9 / 4 = 2 with a remainder of 1. Also called Modulo.

so basicly if the remainder of "year / 4 = 0" and the remainder of "year / 100 is not 0" or "year / 400 has a remainder of 0" the boolean will be True otherwise its False.

System.out.println(isLeapYear); just prints the result that i just talked about.

  •  Tags:  
  • java
  • Related