# How to determine whether a year is leap or not in programming

Leap years are needed to keep our modern day Gregorian calendar in alignment with the Earth's revolutions around the sun. The Gregorian calendar has only 365 days in a year, so if we didn't add a leap day on February 29 nearly every four years, we would lose almost six hours off our calendar every year. After only 100 years, the calendar would be off by around 24 days.

Before writing some code that determines if a year is leap or not, you need to know the math logic behind this task. With the following logic, you can determine whether a year is leap or not

• The year can be evenly divided by 4.
• If the year can be evenly divided by 100, it is NOT a leap year, unless, the year is also evenly divisible by 400. Then it is a leap year.

Now, talking is cheap, so let's get started with some code ! In this way you may understand it better. In the following examples we'll explain how to verify wheter a year is leap or not with different languages, the choosen language for this example will be with the `C Language`:

``````#include <stdio.h>

int main()
{
int year;

// Read the year input e.g 2000
printf("Enter a year: ");
scanf("%d", &year);

if(year % 4 == 0)
{
if(year % 100 == 0)
{
// if year is divisible by 400, then the year is a leap year
if ( year%400 == 0){
printf("%d is a leap year.", year);
}else{
printf("%d is not a leap year.", year);
}
}
else{
printf("%d is a leap year.", year );
}
}else{
printf("%d is not a leap year.", year);
}

return 0;
}``````

The idea is to use the modulo operator to verify if the remainder after the division of the year by 4, 100 and 400 is 0. In short words, if a year is divisible by 4 but not by 100, then the year is leap. The year is leap as well if the year is divisible by 4, divisible by 100 and at the same time divisible by 400.

Happy coding !