Kotlin: Check if a Given Year is a Leap Year

1. Introduction

Leap years have always been an interesting phenomenon in our calendar system. They're the years when February gets an extra day, turning the month into a 29-day affair instead of the usual 28. But how do we determine which years are leap years? In this blog post, we'll discuss the logic behind it and how to check for a leap year using Kotlin.

What is a Leap Year? 

The concept of a leap year exists because our calendar year is not perfectly 365 days. Instead, it's approximately 365.2425 days. To adjust for this discrepancy, we add an extra day, February 29, approximately every four years. 

2. Program Overview

Our Kotlin endeavor will:

1. Take the year as input from the user.

2. Evaluate the provided year based on the leap year conditions.

3. Inform the user whether the entered year is a leap year.

3. Code Program

import java.util.Scanner

fun main() {
    // Engage Scanner for user input
    val reader = Scanner(System.in)

    // Prompt the user to input a year
    print("Enter a year: ")
    val year = reader.nextInt()

    // Check if the year is a leap year
    val isLeap = when {
        // Year is divisible by 4 but not by 100
        year % 4 == 0 && year % 100 != 0 -> true
        // Year is divisible by 400
        year % 400 == 0 -> true
        else -> false

    // Display the result
    if (isLeap) {
        println("$year is a leap year.")
    } else {
        println("$year is not a leap year.")


Enter a year: 2024
2024 is a leap year.

4. Step By Step Explanation

1. Scanner Activation: Our journey starts by initializing the Scanner class, enabling us to capture user input. We name our instance reader.

2. Year Acquisition: The user is urged, via the print function, to provide a specific year.

3. Leap Year Evaluation: Leap years follow specific rules:

  • Generally, if a year is divisible by 4, it's a leap year. However, there's a caveat: years divisible by 100 are not leap years. For instance, the year 1900, though divisible by 4, is not a leap year since it's also divisible by 100.
  • There's an exception to the exception: if a year is divisible by 400, it's a leap year. This means the year 2000 was a leap year as it's divisible by 400.

4. Result Exhibition: The outcome, whether the specified year is a leap year or not, is then presented to the user through the println function.