Program to display next 10 leap years
Program to display next 10 leap years

Program to display next 10 leap years

Posted on

Here is a program in PHP that will display the next 10 leap years

<?php

$currentYear = date("Y");  // get the current year

// loop through the next 10 years
for ($i = 1; $i <= 10; $i++) {
  $year = $currentYear + $i;  // calculate the year
  if ($year % 4 == 0) {  // check if the year is a leap year
    if ($year % 100 == 0) {
      if ($year % 400 == 0) {
        echo $year . " is a leap year\n";  // display the year
      }
    } else {
      echo $year . " is a leap year\n";  // display the year
    }
  }
}

?>

This program first gets the current year using the date function. It then loops through the next 10 years, using the for loop, and calculates the year by adding the loop counter to the current year. The program checks if the year is a leap year by using the modulus operator to check if the year is evenly divisible by 4. If it is, the program checks if the year is evenly divisible by 100, and if it is, it checks if it is also evenly divisible by 400. If the year is evenly divisible by 4 and not by 100, or if it is evenly divisible by 4, 100, and 400, it is considered a leap year and the program displays it.

Program to display next 10 leap years in details

In this program, the current year is obtained using the ‘date‘ function, which returns the current date and time as a string. The 'date' function takes a format string as an argument, and the format string “Y” is used to get the current year as a four-digit number.

The program then enters a 'for loop that will iterate 10 times, with the loop counter starting at 1 and ending at 10. The loop calculates the year for each iteration by adding the loop counter to the current year, which is stored in the ‘$currentYear‘ variable.

Inside the loop, the program uses the modulus operator (%) to check if the year is evenly divisible by 4. If it is, the program checks if the year is evenly divisible by 100. If the year is not evenly divisible by 4 or if it is evenly divisible by 4 and 100, the program skips the rest of the loop and moves on to the next iteration. If the year is evenly divisible by 4 and not by 100, the program assumes that the year is a leap year and displays it using the echo function. If the year is evenly divisible by 4 and 100, the program checks if it is also evenly divisible by 400. If it is, the program assumes that the year is a leap year and displays it. If it is not evenly divisible by 400, the program skips the rest of the loop and moves on to the next iteration.

This program uses the rules for determining leap years, which are as follows:

  • A year is a leap year if it is evenly divisible by 4, except if it is also evenly divisible by 100.
  • A year is a leap year if it is evenly divisible by 4, 100, and 400.

This means that years that are evenly divisible by 4, such as 2020, 2024, etc., are considered leap years unless they are also evenly divisible by 100, in which case they are not considered leap years unless they are also evenly divisible by 400. For example, 1900 is not a leap year because it is evenly divisible by 4 and 100, but not by 400. 2000 is a leap year because it is evenly divisible by 4, 100, and 400.

Leave a Reply