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,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#!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:
1 2 3 4 5 6 7 8 9 |
$ 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!