Write a C program to check if the given year is a Leap year or not.

## What is a Leap Year? 🤔

“A leap year is always divisible by 4 except century years and a century year is a leap year only if it is divisible by 400.”

## Tutorial 📺

Watch tutorial and learn how to solve this problem. 😉

## Steps 🪜

### To check if given year is Leap Year or not

- Accept an integer from the user and store the value in a variable ‘
**year**‘. - Now, validate by checking if the user has entered the valid year value between 1000 and 9999. If the year is not valid then print or display a message to the user, “You’ve entered INVALID YEAR value”.
- After validating the year, check if
**year % 400 == 0**then the year is a leap year. - In else if part, check if
**year % 100 == 0**then print a message that “Year is not a leap year”. - Again, in the else if part, if
**year % 4 == 0**then print “Year is a leap year”. - Finally, in the else part display the message “Year is not a leap year”.

## Flow Chart 🌻

Check how to draw flow chart for this program.

## Code 💻

```
#include <stdio.h>
#include <conio.h>
void main()
{
int year=0;
clrscr();
printf("Please enter the year: ");
scanf("%d", &year);
// Valid Year Range: 1000 - 9999
if(year >= 1000 && year <= 9999)
{
if(year % 400 == 0)
printf("\n%d is a LEAP YEAR.", year);
else if(year % 100 == 0)
printf("\n%d is not a LEAP YEAR.", year);
else if(year % 4 == 0)
printf("\n%d is a LEAP YEAR.", year);
else
printf("\n%d is not a LEAP YEAR.", year);
}
else {
printf("\nYou've entered INVALID YEAR value.");
}
getch();
}
```

## Example 😍

Let’s assume the user enters the value 120 which doesn’t fall in 1000 and 9999, thus a message will be displayed that “You’ve entered INVALID YEAR value”.

But, if the user enters 1992 then it is a valid year value because it falls between 1000 and 9999. Now, it will check whether the entered year value is a leap year or not.

First it will perform modulo operation and check if **year % 400 == 0**. Since 1992 modulo 400 is not equal to 0 then it will go to else-if statement to check if **year % 100 == 0**.

Again, 1992 modulo 100 is not equal to 0 so it will go to next else-if statement to check if **year % 4 == 0**, since 1992 modulo 4 is equal to 0 thus it will execute the printf statement and will display the message that “**1992 is a LEAP YEAR**“.

Let’s take another example where the user enters 1885, as we know it is a valid year since it falls between 1000 and 9999. It will move further to check if it a leap year or not.

First it will perform modulo operation and check if **year % 400 == 0**. Since 1885 modulo 400 is not equal to 0 then it will go to else-if statement to check if **year % 100 == 0**.

Again, 1885 modulo 100 is not equal to 0 so it will go to next else-if statement to check if **year % 4 == 0**, since 1885 modulo 4 is also not equal to 0 thus it will execute printf statement of else-part and will display the message that “**1885 is not a LEAP YEAR**“.

This is how we find out if the year is a Leap year or not.

For more C programs, click here.

## Comments