Dart Example - Program to check Leap year or not

Leap year or not dart or flutter

In this blog post, We will write a program to check 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 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


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 console using Println() function

Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
You'll get a notification every time a post gets published here.

Related posts

Dart| Flutter How to: Read pubspec.yaml attributes (version) with examples

How to Create and build a Singleton Class Dart or Flutter example| Dart By Example

What is the difference between var and dynamic and final in dart?| Flutter By Example

How to: Check if Phone number is valid or not in Dart| Flutter By Example

Dart| Flutter How to read an image from a disk and resize a file

Dart/Flutter: How to write setter and getter fields or members variables in a class with Example

Dart| Flutter How to get extension name and MIME type of a file with example