2 ways in Kotlin to check if a year is a leap year or not

Kotlin program to check if a year is a leap year or not:

In this post, we will learn how to write a program to check if a year is a leap year or not in Kotlin. The program will take the year as input from the user and it will print if that year is a leap year or not.

Before we start to write the program, let’s learn how to check for a leap year.

How to check if a year is a leap year or not:

A year is called a leap year if it is exactly divisible by 4. If it is a century year, it should be perfectly divisible by 400 to be a leap year.

We can use the below algorithm to check if a year is leap year or not:

  • Take the year as input from the user.
  • Check if the year is divisible by 4 or not.
    • If it is not, it is not a leap year
    • If it is, check if it is divisible by 100 or not i.e. if it is a century year or not. If it is a century year, check if it is divisible by 400 or not. If yes, it is a leap year.

Let’s write down the program.

Example 1: Kotlin program to check if a year is a leap year or not by using if-else statements:

Let’s use if-else statements to check if a year is a leap year or not:

import java.util.*

fun main() {
    val scanner = Scanner(System.`in`)
    val isLeapYear: Boolean

    println("Enter a year: ")
    val givenYear = scanner.nextInt()

    isLeapYear = if (givenYear % 4 == 0) {
        if (givenYear % 100 == 0) {
            givenYear % 400 == 0
        } else
            true
    } else
        false

    if (isLeapYear)
        println("It is a leap year")
    else
        println("It is not a leap year")
}

In this example,

  • The scanner is a Scanner object to read the user input data. isLeapYear is a boolean value to define if it is a leap year or not.
  • The isLeapYear variable is used to store a boolean value to define if the year is a leap year or not.
  • It asks the user to enter a year. The scanner object is used to read the year and store it in the givenYear variable.
  • The if-else block is used to check if the entered year, givenYear is a leap year or not. It assigns a boolean value to this variable.
  • The last if-else block checks the value of isLeapYear. If it is true, it is a leap year. Else, it is not.

If you run this program, it will print the output as below:

Enter a year: 
2000
It is a leap year

Enter a year: 
1998
It is not a leap year

Enter a year: 
2010
It is not a leap year

Enter a year: 
2020
It is a leap year

Example 2: Kotlin program to check if a year is a leap year or not by using a when block:

We can also use a when block to write the same program.

import java.util.*

fun main() {
    val scanner = Scanner(System.`in`)
    val isLeapYear: Boolean

    println("Enter a year: ")
    val givenYear = scanner.nextInt()

    isLeapYear = when {
        givenYear % 4 == 0 -> {
            if (givenYear % 100 == 0) {
                givenYear % 400 == 0
            } else
                true
        }

        else -> false
    }

    if (isLeapYear)
        println("It is a leap year")
    else
        println("It is not a leap year")
}

This program is using a when block to check if a year is a leap year or not.

  • If givenYear is perfectly divisible by 4, it checks if it is divisible by 100 or not. Else, it returns false.
    • If it is divisible by 100, it checks if it is divisible by 400 or not and returns the result. Else it returns true.
  • The last if-else block checks the value of isLeapYear and based on its value, it prints a message.

If you run this program, it will print a similar result.

Enter a year: 
2020
It is a leap year

Enter a year: 
2000
It is a leap year

Enter a year: 
1991
It is not a leap year

You might also like: