How to write C Program to Check Leap Year or Not using If Statement, Nested If Statements, and Else If Statement with an example?. Before we get into C program to check leap year, Let us see the logic and definition behind this.

## Leap year

The normal year contains 365 days, but the leap year contains 366 days. This extra day is added to the February month, so we get February 29.

All the years that are perfectly divisible by 4 are called Leap except the century. The Century year means they end with 00, such as 1200, 1300, 2400, 2500, etc. (Obviously, they are divisible by 100). For these, we have to calculate further to check the Leap year.

- If the century year is divisible by 400, then that is a Leap.
- If it is not divisible by 400, then that is not.

## C Program to Check Leap Year using If Statement

This example allows the user to enter any value. And then, this program will check whether the user entered is Leap year or not using the If statement.

#include <stdio.h> int main() { int yr; printf("\n Please Enter any number you wish \n "); scanf("%d", &yr); if (( yr%400 == 0)|| (( yr%4 == 0 ) &&( yr%100 != 0))) printf("\n %d is a Leap. \n", yr); else printf("\n %d is not. \n", yr); return 0; }

```
Please Enter any number you wish
1200
1200 is a Leap.
```

Let us check for the normal one.

```
Please Enter any number you wish
1300
1300 is not.
```

Within this C program to Check the Leap Year example, we used Logical AND and Logical OR operators since we have to check multiple conditions within one If Statement. Let us divide the condition to understand it better.

The first C Programming condition (yr%400 == 0) will check whether the reminder is exactly equal to 0 or not. As per the algorithm, any number that is divisible by 400 is a Leap year.

OR

The second If statement condition holds 2 statements, so they both have to be TRUE.

The first condition (yr%4 == 0) will check whether the remainder of it is exactly equal to 0 or not. If the condition is False, then it will exit from the condition because there is no point in checking the other condition. It is not. AND

The second condition will check (yr% 100) reminder is not equal to 0. If it is TRUE, the given number is not a century number.

As per the algorithm, any number that is divisible by 4 but not by 100, then that number is Leap Year. Please refer to Logical Operators in C to understand the functionality of Logical And and Logical Or.

## C Program to Check Leap Year using Else If Statement

This program allows the user to enter any numeric value. Then this program will check whether the user entered is Leap year or not using the Else If statement

#include <stdio.h> int main() { int yr; printf("\n Please Enter as you wish \n"); scanf("%d", &yr); if ( yr%400 == 0) printf("\n%d is a Leap Year. \n", yr); else if ( yr%100 == 0) printf("\n%d is not. \n", yr); else if ( yr%4 == 0 ) printf("\n%d is a Leap Year. \n", yr); else printf("\n%d is not. \n", yr); return 0; }

```
Please Enter as you wish
2022
2022 is not.
```

In this C Program to Check Leap Year example, the user will enter any number to check. The first If condition will check whether the (yr% 400) reminder is exactly equal to 0. As per the algorithm, any number that is divisible by 400 is a Leap year. If this condition Fails, then it will go to the next condition.

Second If condition will check (yr% 100) reminder is exactly equal to 0 or not. As per the algorithm, any number that is not divisible by 400 but by 100 is Not a Leap year (Century). We checked (yr% 400) in the First If statement. Because it failed, it came to the second condition. If both the first and second condition Fails, then it will go to the third condition.

The third condition will check whether year mod 4 is equal to 0. If this condition is True, the given one is Leap because we already checked for the century years in the previous condition. If all the statements Fail, then it will go to the Else statement at the end. And, if all the above statements fail, then it is not.

## C Program to Check Leap Year using Nested If Statement

This allows the user to enter any number. Next, this program will check whether the user entered is Leap year or not using the Nested If

Within this C Program to Check Leap Year example, the first If condition will find whether the remainder of the (year%4) is exactly equal to 0 or not.

- If the expression is evaluated to False, the given number is not.
- If True, then we have to find further for the century year. So, the compiler will go to the Nested If condition.

Second If condition will check (year%100) reminder is exactly equal to 0 or Not.

- If this condition is False, it is not the century. So it is a Leap year.
- If True, then we have to find whether the number is divisible by 400 or not. So, the compiler will goto to another Nested If.

In this condition, the compiler will check whether the remainder of the (year%400) is exactly equal to 0 or not.

- If the expression result is False, the given number is definitely not.
- If it is True, the given number is a Leap Year.

#include <stdio.h> int main() { int year; printf("\n Please Enter any year you wish \n"); scanf("%d",&year); if(year%4 == 0) { if( year%100 == 0) { if ( year%400 == 0) printf("\n%d is a Leap.", year); else printf("\n%d is not.", year); } else printf("\n%d is a Leap.", year ); } else printf("\n%d is not.", year); return 0; }

Comments are closed.