New update is available. Click here to update.

# C Program to Check Leap Year or Not

Gaurav Joshi
Last Updated: Feb 10, 2023

## Introduction

Today, we will learn how to identify whether a year is a Leap year or not in the C language. But before going ahead with the code, we first need to understand what a Leap year is? A leap year is defined as a year having one additional day. A typical year contains 365 days on the one hand; a leap year has 366 days. The extra day is added to February as a result, after every four years, the month of February has 29 days.

The following properties of a leap year described below will help us distinguish it from a typical year.

• A leap year is divisible by four.
• Not all the years divisible by 4 are leap year exceptions; century years end with 00. Century year is leap only in case they are divisible by 400.

Based on the above properties, 2000, 2004,1996 are leap years, and 1900,1999 are not leap years.

### Program to check Leap Year or not using If Statements.

In the below program, we asked the user for input year and checked using modulus operator if the given year is divisible by 400 or given Year divisible by four but not by 100 if any one of the conditions becomes true, we enter if and print user Entered Year is a leap year.

Code:-

``````// Include header files
#include <stdio.h>
int main()
{

// Declare Variables
int Year;

// Ask User to input Year
printf("\n Enter Year you want to check:- \n ");

// Store user provided input in variable Year
scanf("%d", &Year);

// If statement checks if year is divisible by 400 if not Is Year divisible by 4 but not by 100 . If any one of the condition becomes true we print its Leap year else not a Leap 		Year
if (( Year%400 == 0) || (( Year%4 == 0 ) && ( Year%100 != 0))){
printf("\n Given Year is a Leap Year. \n");
}
else{
printf("\n Given Year is not a Leap Year. \n");
}
return 0;
}``````

Output 01:-

``````Enter Year you want to check:-
2000
Given Year is a Leap Year.``````

Output 02:-

``````Enter Year you want to check:-
1999
Given Year is not a Leap Year. ``````

You can also read about dynamic array in c.

### Program to Check Leap Year using If Else Statements.

In the below program, we have used the same condition to distinguish between a leap year or not, as used in the above program. We have just split the if condition into multiple parts. We ask the user for input. And check if user-provided input is divisible by 400 or not. If the condition becomes true, it prints a Leap year and returns from the main. Otherwise, it goes for the following condition defined in else if statements, i.e. divisibility by 100 and divisibility by 4. If all conditions are false, then we enter else block and execute. At the same time, while using the else if statement, one should keep in mind the order of condition put in else if.

Code:-

``````// Include header files
#include <stdio.h>

// Define main function
int main() {

// Declare int variable
int Year;
printf("Enter Year : \n");
scanf("%d", &Year);

// Based on properties of Leap year we define if a year divisible by 400 is a Leap year.
if (Year % 400 == 0) {
printf("Given Year is a Leap Year.");
}

// Since it escaped the if statement, it means Year is not divisible by 400. and we any year not divisible by 400 but divisible by 100 is not a Leap year
else if (Year % 100 == 0) {
printf("Given Year is not a Leap Year.");
}

// Since it escaped the above two conditions, it's not divisible by 400 and 100. So we check the last one condition for a year to be a leap year, i.e., divisibility by 4
else if (Year % 4 == 0) {
printf("Given Year is a Leap Year.");
}

// Rest all years are not a leap year
else {
printf("Given Year is not a Leap Year.");
}
return 0;
}``````

Output 01:-

``````Enter Year :
2000
Given Year is a Leap Year.``````

Output 02:-

``````Enter Year :
1999
Given Year is not a Leap Year.``````

### Program to check Leap Year or not using Nested If

In the below program, we have used the same condition to distinguish between a leap year or not, as used in the above programs. We have used the if condition inside if statements, i.e., Nested if. We ask the user for input. And check if user-provided input is divisible by four or not. If the condition becomes true, it again checks if it's divisible by 100. If yes, it again uses the if condition to check if a year is divisible by 400. If the statement becomes true, it prints Leap year; otherwise, it executes the respective else block. If the first conditions are false, we directly enter the else block and execute Not a Leap year.

Code:-

``````// Include header files
#include <stdio.h>

// Define main function
int main()
{

// Declare int variable
int Year;

// Asking user for the input
printf("\n Enter Year :-  \n");

// Storing user-input in Variable Year
scanf("%d",&Year);

// Nested if conditions
// First condition checked if Year is divisible by 4 or not
if(Year%4 == 0) {

// If year divisible by 4 we check if it's divisible by 100
if( Year%100 == 0) {

// Since the year is divisible by 100 its a century year we check for divisibility by 400
if ( Year%400 == 0) {
printf("\nGiven Year is a Leap Year.");
} else {
printf("\nGiven Year is not a Leap Year.");
}

//Year divisible by four but not by 100. So it's not a century.
}
else {
printf("\nGiven Year is a Leap Year" );
}
}

//Year is not divisible by 4. The primary condition for any year to be a leap year
else {
printf("\nGiven Year is not a Leap Year.");
}
return 0;
}``````

Output 01:-

``````Enter Year:-
2000
Given Year is a Leap Year.``````

Output 02:-

``````Enter Year:-
1999
Given Year is not a Leap Year.``````

## FAQs

1. How to check if a year is a leap year or not in C language?
Property of Leap year like divisibility by four or divisibility by 400 if it's divisible by 100 are used to check if a year is a leap year or not. Based on the above properties, we can use if-else statements, Nested if statements, Else if statements to evaluate those conditions.

2. What is the vital thing to keep in mind when using Else if statements?
While using the else if statement to evaluate the condition, one should keep in mind the order of condition put in else if statements. Order plays a vital role in Else if statements.

3. What are Nested if statements in the C programming language?
Nested if statements are used to evaluate a condition inside another condition.

## Key Takeaway

In the article, we have extensively discussed the different ways to check if a year asked from the user is a leap year or not. We have explained the C program using if statement or Else-if statements or nested if statement.

We hope this blog has helped you enhance your knowledge regarding the C programming language. To learn more about such content, practice some quality questions that require you to excel at your preparations. Visit our Guided Path in  CodeStudio.To be more confident in data structures and algorithms, try out our DS and Algo Course. Till then, All the best for your future endeavours, and Keep Coding.