Ad space (banner)
๐ŸŸฃJulia Lessons
Lesson 65 / 68

Testing whether a year is a leap year

This lesson covers testing for a leap year in Julia, with the aim of understanding how to write logic combining several conditions.

A year is a leap year if it is divisible by 4 and not by 100, or if it is divisible by 400. It is basic logic that turns up constantly in calendar and date programs.

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

A common early stumble is testing only for multiples of 4. The famous examples are 2000, which is a leap year because it divides by 400, and 1900, which is not because it divides by 100 but not 400.

In professional work, this logic is essential to accurate day counts when implementing date calculations or calendar features.

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

๐Ÿงช This site can't compile or run Julia 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)