{

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 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.

THE BEST NEWSLETTER ANYWHERE
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
Subscribe
You'll get a notification every time a post gets published here.





Related posts

How to convert Double to Integer or Integer to double in Dart| Flutter By Example

Perl How to: Find a Given Variable String is numeric or not

Dart/Flutter: Check if String is Empty, Null, or Blank example

How to generate Unique Id UUID in Dart or Flutter Programming| Dart or Flutterby Example

How to Sort List of numbers or String in ascending and descending in Dart or Flutter example

Dart Enum comparison operator| Enum compareByIndex example| Flutter By Example

Dart Example to count the number of digits in a number| Decimals Count in Dart