Perl script to print Leap years

In this post, we are going to write a simple Perl script to print leap years between two given years.

So basically we have to get two inputs to from the user start year and end year and then loop between the start and end year to print the leap years.

Here is the entire Perl script to print leap years,

#!usr/bin/perl

# print a statement about the program 
print "Perl Script to Print Leap years!\n\n";
 
# get the start and end year from user
print "Enter Start Year: ";
$startYear=<STDIN>;
print "Enter End Year: ";
$endYear=<STDIN>;

print "Leap years are:\n";

# loop through between the start and end year
for($i=$startYear; $i <= $endYear; $i++){
  if( (0 == $i % 4) && (0 != $i % 100) || (0 == $i % 400) ){
    print "${i}\n";
  }
}

Just go through the inline comments to understand it.

Sample output of the above program:

$ perl perltest.pl
Perl Script to Print Leap years!

Enter Start Year: 2019
Enter End Year: 2030
Leap years are:
2020
2024
2028

Happy coding!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top