Kotlin Program to Check Leap Year

Determining whether a year is a leap year is a common programming task. A leap year is a year that is evenly divisible by 4, but years divisible by 100 are not leap years, unless they are also divisible by 400. In this article, we will explore Kotlin Program to Check Leap Year with three different solutions. Each solution will be explained and accompanied by sample outputs.

1. Using Basic Conditional Statements

In this section, we’ll use basic conditional statements to check if a year is a leap year.

1.1 Leap Year Check with if-else

Kotlin
fun isLeapYear(year: Int): Boolean {
    return if (year % 4 == 0) {
        if (year % 100 == 0) {
            year % 400 == 0
        } else {
            true
        }
    } else {
        false
    }
}

fun main() {
    val year = 2020
    if (isLeapYear(year)) {
        println("$year is a leap year.")
    } else {
        println("$year is not a leap year.")
    }
}

Output:

Kotlin
2020 is a leap year.

Explanation: This approach uses nested if-else statements to determine if a year is a leap year. It first checks if the year is divisible by 4, then by 100, and finally by 400 if necessary.

1.2 Leap Year Check with Simplified Conditions

Kotlin
fun isLeapYear(year: Int): Boolean {
    return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)
}

fun main() {
    val year = 1900
    if (isLeapYear(year)) {
        println("$year is a leap year.")
    } else {
        println("$year is not a leap year.")
    }
}

Output:

Kotlin
1900 is not a leap year.

Explanation: This approach simplifies the conditions using logical operators to check for a leap year in a single return statement.

2. Using Extension Functions

In this section, we’ll extend the Int class to include a isLeapYear function for a more idiomatic Kotlin approach.

2.1 Leap Year Check with Extension Function

Kotlin
fun Int.isLeapYear(): Boolean {
    return (this % 4 == 0 && this % 100 != 0) || (this % 400 == 0)
}

fun main() {
    val year = 2000
    if (year.isLeapYear()) {
        println("$year is a leap year.")
    } else {
        println("$year is not a leap year.")
    }
}

Output:

Kotlin
2000 is a leap year.

Explanation: This approach extends the Int class with an isLeapYear function, making it possible to call the function directly on an integer representing the year.

2.2 Leap Year Check with Extension Function and Custom Message

Kotlin
fun Int.isLeapYear(): Boolean {
    return (this % 4 == 0 && this % 100 != 0) || (this % 400 == 0)
}

fun Int.leapYearMessage(): String {
    return if (this.isLeapYear()) {
        "$this is a leap year."
    } else {
        "$this is not a leap year."
    }
}

fun main() {
    val year = 2021
    println(year.leapYearMessage())
}

Output:

Kotlin
2021 is not a leap year.

Explanation: This approach builds on the previous extension function by adding a custom message function that returns a formatted string indicating whether the year is a leap year.

3. Using Java Time API

In this section, we’ll leverage the Java Time API for checking leap years, which is especially useful for more complex date and time manipulations.

3.1 Leap Year Check Using Year Class

Kotlin
import java.time.Year

fun main() {
    val year = 2024
    if (Year.isLeap(year.toLong())) {
        println("$year is a leap year.")
    } else {
        println("$year is not a leap year.")
    }
}

Output:

Kotlin
2024 is a leap year.

Explanation: This approach uses the Year class from the Java Time API to check if a year is a leap year. This method is beneficial when working within applications that already utilize the Java Time API.

Conclusion

In this article, we explored various methods to check if a year is a leap year using Kotlin. We used basic conditional statements, simplified conditions, extension functions, and the Java Time API. Each method has its use cases, and the choice depends on the specific needs of your application. By understanding these different approaches, you can select the most appropriate one for your Kotlin projects.