Leap Year Program in JavaIn this tutorial, we are going to discuss how to identify whether a given year is a leap year or not. But before proceeding further, we will be discussing the leap year. Leap YearA leap year, also known as a bissextile or intercalary year, contains 1 more additional day (a total of 366 days) as compare to other years. Usually, it comes at a gap of 4 years. 2008, 2012, 2016, etc., are some examples of leap years. Rules to verify
For example, 1996 is a leap year, and 1999 is not a leap year, as the former one is divisible by 4 and the latter one is not divisible by 4. Implementation: Without Using Scanner ClassObserve the implementation of the above rules. FileName: LeapYear.java Output: The year 1996 is a leap year. The year 1999 is not a leap year. The year 1700 is not a leap year. The year 2000 is a leap year. Complexity Analysis: The time complexity of the program is O(1). The space complexity of the program is also O(1). Implementation: With Using Scanner ClassHere, the user is provided the flexibility to enter the year of his/her choice, and then the program tests whether the entered number is a leap year or not. Also, instead of multiple if statements, we can use only one if do our job. The implementation of it is mentioned in the following program. FileName: LeapYear1.java Output: Enter Year 1996 The year 1996 is a leap year. Enter Year 1999 The year 1999 is not a leap year. Enter Year 1700 The year 1700 is not a leap year. Enter Year 2000 The year 2000 is a leap year. Complexity Analysis: The time complexity of the program is O(1). The space complexity of the program is also O(1). Implementation: Finding Leap Years Within a RangeWe can also find leap years within a given range with the help of a for-loop. Observe the following program. FileName: LeapYear2.java Output: Finding leap years within the range 2000 and 2150 2000 2004 2008 2012 2016 2020 2024 2028 2032 2036 2040 2044 2048 2052 2056 2060 2064 2068 2072 2076 2080 2084 2088 2092 2096 2104 2108 2112 2116 2120 2124 2128 2132 2136 2140 2144 2148 Complexity Analysis: The space and time complexity of the program is the same as the previous program. Implementation: Using Strings for Finding the RangeWe can also convert the years into strings and check for the leap years. See the following code. FileName: LeapYear3.java Output: Finding leap years within the range 2000 and 2150 2000 2004 2008 2012 2016 2020 2024 2028 2032 2036 2040 2044 2048 2052 2056 2060 2064 2068 2072 2076 2080 2084 2088 2092 2096 2104 2108 2112 2116 2120 2124 2128 2132 2136 2140 2144 2148 Complexity Analysis: The program is converting the integer into String, that takes O(D) times. Also, program is storing that String using variable y, that also takes O(D) space. Therefore, the time as well the space complexity of the program is O(D), where D is the total number of digits present in the year for which we are checking leap year. Note: The implementation using String is worst in terms of space and time complexities as compared to other implementations. However, it may come in handy if the input array, which is containing the years, is of type String.
Next TopicOnline Java Compiler GDB
|