Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is a Leap Year?
3.
How to Find Leap Year Using C Programming?
4.
Pseudocode for a Leap Year C Program
5.
Program to check Leap Year or not using If Statements.
5.1.
Implementation
5.2.
C
6.
Program to Check Leap Year using If Else Statements.
6.1.
Implementation
6.2.
C
7.
Program to check Leap Year or not using Nested If 
7.1.
Implementation
7.2.
C
8.
C Program to Find Leap Years Within a Given Range
8.1.
C
9.
Frequently Asked Questions
9.1.
How to check if a year is a leap year or not in C language?
9.2.
What is the vital thing to keep in mind when using Else if statements?
9.3.
What are Nested if statements in the C programming language?
9.4.
What is the formula for leap year?
10.
Conclusion
Last Updated: Aug 11, 2024
Easy

C Program to Check Leap Year or Not

Author Gaurav joshi
0 upvote

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.
C Program to Check Leap Year or Not

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

Also see: C Static Function, and Tribonacci Series

What is a Leap Year?

A leap year is a year that has one extra day, making it 366 days long instead of the usual 365 days. This extra day is added to February, which has 29 days in a leap year instead of 28. Leap years occur every 4 years to correct for the fact that a year is not exactly 365.25 days long but approximately 365.2425 days. However, there are exceptions: a year is a leap year if it is divisible by 4, but years divisible by 100 are not leap years unless they are also divisible by 400.

How to Find Leap Year Using C Programming?

To determine whether a year is a leap year in C, follow these steps:

  1. A year divisible by 400 is a leap year.
  2. A year divisible by 100 but not by 400 is not a leap year.
  3. A year divisible by 4 but not by 100 is a leap year.
  4. A year not divisible by 4 is not a leap year.

You can implement these checks in a C program to evaluate whether a given year is a leap year or not.

Pseudocode for a Leap Year C Program

  1. Start
  2. Input: Read the year from the user.
  3. Check Conditions:
    • If the year is divisible by 400, it is a leap year.
    • Else if the year is divisible by 100, it is not a leap year.
    • Else if the year is divisible by 4, it is a leap year.
    • Otherwise, it is not a leap year.
  4. Output: Print whether the year is a leap year or not.
  5. End

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.

Implementation

  • C

C

// 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;
}
You can also try this code with Online C Compiler
Run Code

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 and Short int in C Programming

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.

Implementation

  • C

C

// 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;
}
You can also try this code with Online C Compiler
Run Code

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.

Implementation

  • C

C

// 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;
}
You can also try this code with Online C Compiler
Run Code

Output 01:-

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

Output 02:-

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

C Program to Find Leap Years Within a Given Range

To find all the leap years within a given range, you can write a C program that iterates through each year in the range and applies the leap year conditions to determine if a year is a leap year. The program will then print all the leap years that fall within the specified range.

Example:

  • C

C

#include <stdio.h>

int main() {
int startYear, endYear;

// Input start and end year
printf("Enter the start year: ");
scanf("%d", &startYear);
printf("Enter the end year: ");
scanf("%d", &endYear);

// Print leap years within the range
printf("Leap years between %d and %d are:\n", startYear, endYear);
for (int year = startYear; year <= endYear; year++) {
if ((year % 400 == 0) || (year % 100 != 0 && year % 4 == 0)) {
printf("%d\n", year);
}
}

return 0;
}
You can also try this code with Online C Compiler
Run Code

 

Output:

Enter the start year: 2000
Enter the end year: 2020
Leap years between 2000 and 2020 are:
2000
2004
2008
2012
2016

Frequently Asked Questions

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.

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.

What are Nested if statements in the C programming language?

Nested if statements are used to evaluate a condition inside another condition.

What is the formula for leap year?

A year is a leap year if:

  1. It is divisible by 400, or
  2. It is divisible by 4 but not by 100.

Conclusion

In the article, we have extensively discussed the C Program to Check Leap Year or Not. Determining whether a year is a leap year is a fundamental task that highlights key concepts in programming, such as conditional statements and modular arithmetic. The C program provided demonstrates a straightforward approach to checking leap years by evaluating specific conditions. By understanding and implementing these checks, you can effectively handle date-related calculations and improve your programming skills.

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  Coding Ninjas Studio.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.

Live masterclass