{

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

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 console using 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

Dart| Flutter How to: Find a Given String is numeric or not

How to iterate loop Enum in Dart | Get Enum with index in Flutter By Example

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

Dart tutorial examples/ Flutter By Examples

How to add Methods or Values to Enums in Dart | Flutter By Example

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

How to: Enum to String in Dart | Print Enum name as String in Flutter By Example