Sunday, July 16, 2017

Write a Java method to check whether a year (integer) entered by the user is a leap year or not.

/**
 * Created by Jo on 7/10/2017.
 * For more exercises and lessons, visit http://shegertech.blogspot.com/
 * Chapter 8, Exercise 2
 */

public class Q2Solution {
    public static void main(String[] args){
        int year = 2008;

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

Share: