Leap Year C Program to Check if the User-Entered Year is a Leap Year

You can write a leap year program in C to check if the given year is a leap year or not. But before we proceed, let us first understand what a leap year is. 

What is a Leap Year?

A leap year is a year that occurs once every four years. Based on the Gregorian calendar, the theory is that a year is around 365.25 days long. But since we consider a year to be 365 days, the additional 0.25 days are added to the next calendar year, which brings the total to 1 day in the fourth year. Hence, instead of 365 days, a leap year is 366 days long. The additional day is added as the 29th of February.

How to Find a Leap Year Using C

To find whether a year is a leap year or not using a leap year C program, all you need to do is enter some conditions (mathematical) in the program code with the help of If… Else statement; Following are the conditions to check if the given year is a leap year or not:

  • The entered year must be divisible by 4
  • The entered year must be divisible by 400 but not by 100

The second condition is used to segregate the century years from leap years. Century years are the ones with the ’00s in the end, for instance, 1300, 1500, 1400 and likewise. A century year is considered to be a leap year only if it is evenly divisible by 400. For example, the years 1200, 1600, and 2000 are all century leap years since these numbers are perfectly divisible by 400.

Add Another Star to Your Performance Evaluation

Learn from industry experts for FREEStart Learning
Add Another Star to Your Performance Evaluation

Flow Diagram of a Leap Year C Program

Below is a flow diagram of how to write a leap year C program.

Leap_Year_C_Program_1.

*[&& is logical AND-operator / II is logical OR-operator]

Pseudocode for a Leap Year C Program

You can write a pseudocode for the leap year C program as below:

Start procedure leap_year()   

   If year % 4 = 0 AND year % 100!= 0 OR year%400 == 0

      Print leap year

   Else

      Print not leap year

   End IF

End procedure

How to Implement Leap Year C Program

We will now use the above pseudocode to write a leap year C program. After implementing the code, we will provide different years on each execution to see the results. Here’s how you can implement the code in C programming.

#include <stdio.h>

int main(){

   int y; 

   printf("Enter the year to check: ");

   scanf("%d",&y);

   if (((y % 4 == 0) && (y % 100!= 0)) || (y%400 == 0))

      printf("It is a leap year");

   else

      printf("It is not a leap year");

   return 0;

}

Output 1:

Leap_Year_C_Program_2

It is a leap year as the year 2016 is divisible by 4 but not divisible by 100.

Output 2:

Leap_Year_C_Program_3

It is not a leap year as the year 2013 is not divisible by 4.

Output 3:

Leap_Year_C_Program_4

It is a leap year as the year 1200 is divisible by 400.

Output 4:

Leap_Year_C_Program_5

It is not a leap year as the year 1500 is divisible by 4 and 100, making it a century year.

Full Stack Web Developer Course

To become an expert in MEAN StackView Course
Full Stack Web Developer Course

Leap Year C Program to Find Leap Years Within a Given Range

In the previous example, we asked the user to input a year and checked if it is a leap year or not. But here, we will write a leap year C program to ask the user to input a range and print all the leap years between the range.

#include <stdio.h>

int main(){

    int start_Year, end_Year, i;   

    printf("Enter the starting year of the range: ");

    scanf("%d",&start_Year);   

    printf("Enter the last year of the range: ");

    scanf("%d",&end_Year);

    //the given range

    printf("Leap Years between %d and %d are: \n", start_Year, end_Year);

    for (i= start_Year; i<= end_Year; i++){

        if (((i % 4 == 0) && (i % 100!= 0)) || (i % 400 == 0)){

            printf("%d \n", i);

        }

    }    

    return 0;

}

Output:

Leap_Year_C_Program_6

That was all about the leap year C program

Advance your career as a MEAN stack developer with the Full Stack Web Developer - MEAN Stack Master's Program. Enroll now!

Conclusion

You can use a similar concept to find out different ranges of years. The leap year C program is also sometimes used as an introduction point to the if-else statements in C. If you want to learn more about C fundamentals, such as pointers and loops, sign up on Simplilearn’s SkillUp platform. The platform provides a wide range of free online courses to help you clear the basics of multiple programming languages, including C.

Besides learning about C programming, you should also master some other popular programming languages because multinational companies prefer a full-stack developer in today’s competitive times. Hence, it is essential and recommended that you increase your knowledge, skills, and scope of work. You can do all of that by simply opting for Simplilearn’s Full-Stack Web Development Certification Course. The course will help you get acquainted with a wide array of programming languages and relevant tools to help you pursue a software development career.  

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.