0
0
KotlinProgramBeginner · 2 min read

Kotlin Program to Check Leap Year

You can check a leap year in Kotlin using if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) to test the year and print the result.
📋

Examples

Input2000
Output2000 is a leap year
Input1900
Output1900 is not a leap year
Input2024
Output2024 is a leap year
🧠

How to Think About It

To check if a year is leap, first see if it divides evenly by 400. If yes, it is leap. If not, check if it divides evenly by 4 but not by 100. If yes, it is leap. Otherwise, it is not leap.
📐

Algorithm

1
Get the year as input
2
Check if year is divisible by 400; if yes, it is leap
3
Else check if year is divisible by 4 and not by 100; if yes, it is leap
4
Otherwise, it is not a leap year
5
Print the result
💻

Code

kotlin
fun main() {
    val year = 2024
    if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
        println("$year is a leap year")
    } else {
        println("$year is not a leap year")
    }
}
Output
2024 is a leap year
🔍

Dry Run

Let's trace the year 1900 through the code

1

Input year

year = 1900

2

Check divisibility by 400

1900 % 400 = 300 (not zero), so false

3

Check divisibility by 4 and not by 100

1900 % 4 = 0 (true) and 1900 % 100 = 0 (true), so condition false because year % 100 == 0

4

Determine result

Year is not a leap year

StepConditionResult
Divisible by 400?1900 % 400 == 0false
Divisible by 4?1900 % 4 == 0true
Not divisible by 100?1900 % 100 != 0false
Leap year?false || (true && false)false
💡

Why This Works

Step 1: Divisible by 400 means leap year

If a year divides evenly by 400, it is always a leap year, so we check year % 400 == 0 first.

Step 2: Divisible by 4 but not by 100 means leap year

If the year is divisible by 4 but not by 100, it is a leap year, checked by year % 4 == 0 && year % 100 != 0.

Step 3: Otherwise, not a leap year

If neither condition is true, the year is not a leap year.

🔄

Alternative Approaches

Using when expression
kotlin
fun main() {
    val year = 2024
    when {
        year % 400 == 0 -> println("$year is a leap year")
        year % 4 == 0 && year % 100 != 0 -> println("$year is a leap year")
        else -> println("$year is not a leap year")
    }
}
This uses Kotlin's when expression for clearer branching but is functionally the same.
Function returning Boolean
kotlin
fun isLeapYear(year: Int): Boolean {
    return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)
}

fun main() {
    val year = 2024
    if (isLeapYear(year)) println("$year is a leap year") else println("$year is not a leap year")
}
Separates logic into a reusable function, improving readability and reusability.

Complexity: O(1) time, O(1) space

Time Complexity

The program performs a fixed number of arithmetic and logical operations regardless of input size, so it runs in constant time O(1).

Space Complexity

It uses a fixed amount of memory for variables and no extra data structures, so space complexity is O(1).

Which Approach is Fastest?

All approaches use simple arithmetic checks and run in constant time; using a function improves code reuse but does not affect speed.

ApproachTimeSpaceBest For
If-else conditionO(1)O(1)Simple and direct checks
When expressionO(1)O(1)Clear branching syntax
Function returning BooleanO(1)O(1)Reusable and clean code
💡
Remember to check divisibility by 400 first because it overrides the other conditions.
⚠️
Beginners often forget to exclude years divisible by 100 but not by 400, causing wrong leap year results.