Home »
Kotlin »
Kotlin Programs »
Kotlin Basic Programs
Kotlin program to check leap year
Kotlin | Checking leap year: Here, we are going to learn how to check whether a given year is a leap year or not in Kotlin?
Submitted by IncludeHelp, on April 17, 2020
What is a Leap Year?
"A leap year is a calendar year that contains an additional day added to keep the calendar year synchronized with the astronomical year or seasonal year." ~ Wikipedia
A Non-Leap year contains 365 days in a year while a Leap Year contains 366 days in a year.
In terms of programming logic, a leap year,
- should be divisible by 400 (In case of century years) .
- and, should be divisible by 4 (In case of non-century years).
Problem statement
Given a year, we have to check whether it is a leap year or not.
Example:
Input:
2020
Output:
2020 is Leap Year
Input:
2021
Output:
2021 is Not a Leap Year
Program to check leap year in Kotlin
package com.includehelp.basic
import java.util.*
// Main Method Entry Point of Program
fun main(args: Array<String>) {
// InputStream to get Input
var reader = Scanner(System.`in`)
//Input Integer Value
println("Enter Year : ")
var year = reader.nextInt();
// checking leap year condition
val isleap = if (year % 4 == 0){
if (year % 100 == 0) {
// Century Year must be divisible by 400 for Leap year
year % 400 == 0
} else true
} else false;
println("$year is ${if (isleap) "Leap Year" else "Not a Leap Year"} ")
}
Output
Run 1:
Enter Year :
2020
2020 is Leap Year
-------
Run 2:
Enter Year :
1987
1987 is Not a Leap Year
----
Run 3:
Enter Year :
2012
2012 is Leap Year
----
Run 4:
Enter Year :
1900
1900 is Not a Leap Year