-4

I'm trying to learn Java. When I enter 1800 which is not a leap year, it says "Leap year" but if I enter 2011 which is not a leap year it says "not leap year".

anyone that can help? I'm new to Java so I'm learning all this. The problem I'm trying to solve is noted below.

A year is a leap year if it is divisible by 4. However, if the year is divisible by 100, then it is a leap year only when it is also divisible by 400.

import java.util.Scanner;
    
public class leapyears {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter a year!");
        int year = Integer.valueOf(scanner.nextLine());


        if (year % 4 == 0) {

            System.out.println("Leap year!");

            if (year % 100 != 0 && year % 400 == 0) {

                System.out.println("Leap year!");

            }
        } else {
            System.out.println("Not leap year!");
        }
    }
}
Turing85
  • 18,217
  • 7
  • 33
  • 58
Sedrido
  • 23
  • 4
  • https://www.tutorialspoint.com/Java-program-to-find-if-the-given-number-is-a-leap-year – HarryQ Sep 08 '20 at 14:51
  • 5
    Your logic doesn't make sense. You print "Leap year!" before you even check if the number is divisible by 100. – khelwood Sep 08 '20 at 14:53
  • 1
    Yes -- this isn't really a programming problem, as such, but a matter of understanding the logic. Not all years divisible by 4 are leap years, so you need to test the special cases before you test the general case. – Kevin Boone Sep 08 '20 at 14:54
  • 1
    Your code implements the following algorithm : if the year isn't divisible by 4, print "not leap year" (that part is good), otherwise (if the year is divisible by 4) print "leap year" and if it's not divisible by 100 but it is divisible by 400 (which will never happen) print "leap year" a second time – Aaron Sep 08 '20 at 14:55
  • You should research before asking a question, perhaps your answer is here https://stackoverflow.com/questions/1021324/java-code-for-calculating-leap-year/34929299 – Abraham Sep 08 '20 at 16:14

4 Answers4

3

Don't reinvent the wheel, Java already does it for you!

java.time.Year::isLeap

import java.time.Year;

public class Main
{
    public static void main(String[] args) {
        System.out.println(Year.of(2020).isLeap());
    }
}
Abraham
  • 8,525
  • 5
  • 47
  • 53
2

You can write a simple method which also facilitates testing.

for (int year : new int[] { 1843, 1900, 2000, 2004, 2200,
            2400 }) {
    System.out.printf("%d is%sa leap year.%n", year,
                    isLeapYear(year) ? " " : " not ");
    }
}

// non century years divisible by 4 or century years divisible by 400
public static boolean isLeapYear(int year) {
    return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
}

You can also use the library method to check your own method.

import java.time.Year;
for (int year = 2000; year <= 3000; year++) {
    if (Year.isLeap(year) != isLeapYear(year)) {
        System.out.println("Oops - " + year);
    }
}
WJS
  • 36,363
  • 4
  • 24
  • 39
1

I think your condition is incorrect. year % 100 !=0 and year % 400 ==0 cannot be met at the same time. Try the following.

   if (year % 400 == 0 || ((year % 4 ==0) && (year %100 != 0)) {
                   //  ^ this is OR      //^ this is AND

        System.out.println("Leap year!");
   else{
        System.out.println("Not leap year!");
    }
HarryQ
  • 1,281
  • 2
  • 10
  • 25
1

for starters, this is not a problem with the code, this is the problem of you not understanding the logic behind the leap year program.(when i started doing java, which was not so long ago I had the same problem too):D`

the important flaw that u had in your problem is this you checked for the modulus of 4 before checking it for 100, coz a number divisible by 4 wont always be divisible by 100 but the converse is true though.

let's say the year you entered was 1800, it will be divisible by 4, which means the if statement is true and the the block is executed.(this is wrong tho)

what you should consider doing is

  1. check for the divisibility of 100 first and after that you can check if the number is divisible by 4 or 400

I've pinned a code for you to understand

import java.util.Scanner;

public class leapyear {

public static void main(String[] args) {

    Scanner s = new Scanner(System.in);
    
    System.out.print("Enter the year:");
    int yr = s.nextInt();
    
    if(yr%100==0)
    {
        if(yr%400==0)
        {
            System.out.println(yr+" "+"is a leap year");
        }
        else
        {
            System.out.println(yr+" "+"is not a leap year");
        }
        
    }

    else if(yr%4==0)
    {
        System.out.println(yr+" "+"is an leap year");
    }
    
    else
    {
        System.out.println(yr+" "+"is not a leap year");
    }
}

}

hope this solves your problem....(:

p.s this is my first answer and Im very new to programming. So if i made any mistakes feel free to lemme know and if you want to make any update to my existing code you are most welcomed..thank you for taking your time to read this answer of mine

  • 1
    format your code, answers need to be to the point, please cleanup your answer. – Abhilash Sep 08 '20 at 22:06
  • ... and avoid "u", "coz", and "lemme". It's not hard to write "you", "because" and "let me" and makes it *a lot* easier to read. – Matthieu Mar 21 '21 at 15:40