AMCgiftcard
AMCgiftcard

Reputation: 7

Having trouble writing code that can calculate the leap year

Directions: "A year is a leap year if it is divisible by 4. But if the year is divisible by 100, it is a leap year only when it is also divisible by 400.

Create a program that checks whether the given year is a leap year." I am having most trouble with getting the calculations part down. Here is my code:

public static void main(String[] args) {
    Scanner reader = new Scanner(System.in);

    int year;

    System.out.print("Type a year:");
    year=Integer.parseInt(reader.nextLine());

    if ((year%4==0)||(((year%100==year%400)))){

      System.out.println("The year a leap year.");
 }else       
       System.out.println("The year is not a leap year.");

}

I don't receive errors when I run the code, however, when I put such dates, 1600, 1601, and 1700 etc it says it is a leap year when it should not be. I'm very lost and any help will be greatly appreciated

Upvotes: 0

Views: 85

Answers (1)

ECLIPSE-MAN
ECLIPSE-MAN

Reputation: 76

Try this:

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

Upvotes: 1

Related Questions