Problem 2: Leap Years Print out all the leap years between a given starting year and ending year. If the starting year is greater than the ending year or one of the inputs is negative, an appropriate error message should be printed. .java = LeapYears.java Examples (a) printLeapYears(2000, 2010) prints out: 2000 2004 2008 (b) printLeapYears(2000, 2000) prints out: 2000 (c) printLeapYears(
public class LeapYears { public static boolean isLeap(int year){ if(year % 4 == 0 && year % 100 != 0){ return true; } else if(year % 400 == 0){ return true; } return false; } public static void printLeapYears(int year1, int year2){ for(int i = year1;i<=year2;i++){ if(isLeap(i)){ System.out.print(i+" "); } } System.out.println(); } public static void main(String[] args) { // Testing printLeapYears(2000, 2010); printLeapYears(2000, 2000); } }
Get Answers For Free
Most questions answered within 1 hours.