Ad space (banner)
๐Ÿ”ทVB.NET Lessons
Lesson 65 / 68

Testing whether a year is a leap year

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

A leap year follows a slightly awkward rule: divisible by 4 but not by 100, or divisible by 400. Testing only for multiples of 4 gets some years wrong.

The sample code expresses that whole rule in the single expression (year Mod 4 = 0 AndAlso year Mod 100 <> 0) OrElse year Mod 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 divisibility by 4. Exceptions such as 1900 — a multiple of 4 that is nonetheless not a leap year — mean the 100 and 400 conditions have to be checked as well.

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 VB.NET 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)