Dart Example - Program to check Leap year or not

In this blog post, We will write a program to check whether a given year is a leap year or not in dart and flutter.

How do you check if the year is a leap year or not?

  1. if the year is divisible by 4, check step 2, else go to step 5
  2. if the year is divisible by 100, check step 3 else go to step 4
  3. if the year is divisible by 400, check step 4, else go to step 5
  4. Then the year is leap year which has 366 days
  5. This is not leap year which has 365 days

Finally, if the year meets all the above conditions, Then it is a Leap year.

How to check given year is a leap year or not in dart

Dart language provides the following features.

Using && and || operators, We can leap year as shown below Following is an example code.

void main() {
  print(isLeapYear(1990)); // false
  print(isLeapYear(1991)); // false
  print(isLeapYear(1992)); // true
  print(isLeapYear(1993)); // false
}

bool isLeapYear(int year) =>
    (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0));

The above program is compiled and the output is

false
false
true
false

In the above program, Created a function isLeapYear which accepts parameter year of type int and returns true, if it is a leap year, else false - if not leap year

Since 1992 is divisible by 4 and not divisible by 100, and 1980 is a leap year. But 1990,1991,1993 is not divisible by 4, so these are not leap year.

Finally,display the Boolean value to the console using the Println() function.