Ad space (banner)
๐ŸŽฏKotlin Lessons
Lesson 65 / 68

Checking Whether a Year Is a Leap Year

In this lesson you will learn how to test whether a year is a leap year in Kotlin, covering how to combine several conditions into one decision. This is for anyone searching for "Kotlin leap year check implementation".

A year is a leap year if it is divisible by 4 but not by 100, or if it is divisible by 400. It is basic logic that appears in calendar and date-calculation programs.

The sample code expresses this whole rule in the single expression (year % 4 == 0 && year % 100 != 0) || year % 400 == 0. 2024 is divisible by 4 and not by 100, so it is a leap year; 1900 is divisible by 100 but not by 400, so it is not.

A common stumbling block for beginners is testing only for multiples of 4. The famous examples are 2000, which is divisible by 400 and so is a leap year, and 1900, which is divisible by 100 but not 400 and so is not.

In real-world development this leap-year logic is essential for accurate day counts whenever you implement date arithmetic or calendar features.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Kotlin directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)