C program for leap year – Example with logic, description and C syntax expression to find a leap year.

**Simple logic to find Leap year?**

- Find if the year is perfectly divisible by 4. If not divisible, then not a leap year.
- If divisible by 4 then,
**it can be a leap year**. So, now, check if it is perfectly divisible by 100. - If not divisible by 100 then
**it is a leap year**, but, If it is divisible by 100 then**it can be a leap year**, So, now, check if the year is divisible by 400, if divisible then**it is a leap year**or else not a leap year.

Here are some years that can be checked with above logic whether they are leap year or not.

**NOT A LEAP YEARS:**1700, 1800, 1900, 1971 and 2006**LEAP YEARS :**1200, 1600, 2000, 2400 ,2000, 2004, 2008, 2012, 2016, 2020, 2024, 2028, 2032, 2036, 2040, 2044, and 2048.

*The same years can be tested by executing the C program given below.*

**Example of C program for leap year**

```
/* C program to check if a year is leap year*/
#include <stdio.h>
//function receives a year and find if
// the year is a leap year
//returns 1 if leap year
//returns 0 if not a leap year
int isLeapYear(int year){
int isLeap = 0;//false
//-----The year is divisible by 4 ? -----------
//YES - can be leap year
//NO - Its not a leap year
if(year % 4 == 0)
{
//-------------is year divisible by 100?---
//YES - can be a leap year
//NO - it is a leap year
if( year % 100 == 0)
{
//---------is number divisible by 400----
//YES - IT IS A leap year
//NO - it is NOT a leap year
if( year % 400 == 0)
{
isLeap = 1;
}else
{
isLeap =0;
}
}else
{
//it is a leap year
isLeap = 1;
}
}else
{
//number is not a leap year if not divisible by 4
isLeap = 0;
}
return isLeap;
}
int main()
{
int year;
int leap = 0;
//Enter the year and read in year variable using scanf
//function.
printf("Enter a year: ");
scanf("%d",&year);
leap = isLeapYear(year);
if (leap)
{
printf("%d is a leap year.", year);
}else
{
printf("%d is not a leap year", year);
}
return 0;
}
```

**Output:**

To check the output you can run the program and enter the following years to test whether they are leap year or not.

Below years will output as the year is not a leap year

1700, 1800, 1900, 1971 and 2006

Below years will output as the year is a leap year

1200, 1600, 2000, 2400 ,2000, 2004, 2008, 2012, 2016, 2020, 2024, 2028, 2032, 2036, 2040, 2044, and 2048.

**NOTE:**

In the function if else conditional expression can be also written as one liner expression in the program. Here is the code example.

```
int main()
{
int year;
printf("Enter a year: ");
scanf("%d",&year);
//one liner expression to find leap year
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
printf("%d is a leap year.", year);
} else {
printf("%d is not a leap year", year);
}
return 0;
}
```