C Program Find If Leap Year

By | March 7, 2020

What is a Leap Year?

A leap year is a calendar year that contains an additional day. For example, in the Gregorian calendar, each leap year has 366 days instead of 365, by extending February to 29 days rather than the common 28.

These extra days occur in each year which is an integer multiple of 4 (except for years evenly divisible by 100, which are not leap years unless evenly divisible by 400).

February 29 is a date that usually occurs every four years, and is called the leap day. This day is added to the calendar in leap years as a corrective measure, because the Earth does not orbit the sun in precisely 365 days.

Algorithm for Leap Year Check

if ((year modulo 4 is 0) and (year modulo 100 is not 0))
or (year modulo 400 is 0)
then true
else false

In C++ Programming Language Conditions for Leap Year

if( (year%400==0 || year%100!=0) &&(year%4==0))
cout<<“It is a leap year”;
else
cout<<“It is not a leap year”;

Piece of C Language Code for checking a Leap Year

if( (year%400==0 || year%100!=0) &&(year%4==0))
printf(“It is a leap year”);
else
printf(“It is not a leap year”);

General algorithm for calculating a leap year

In general terms the algorithm for calculating a leap year is as follows…

A year will be a leap year if it is divisible by 4 but not by 100. If a year is divisible by 4 and by 100, it is not a leap year unless it is also divisible by 400.

Examples of Leap Years or Not a Leap Year

Thus years such as 1996, 1992, 1988 and so on are leap years because they are divisible by 4 but not by 100. For century years, the 400 rule is important. Thus, century years 1900, 1800 and 1700 while all still divisible by 4 are also exactly divisible by 100. As they are not further divisible by 400, they are not leap years

C Program to check for Leap Year

#include<stdio.h>
int main()
{
    int year;
    printf("Enter a year to check if it is a leap year=");
    scanf("%d",&year);
    if(year%400 == 0)
        printf("%d is a leap year", year);
    else if(year%100 == 0)
        printf("%d is not a leap year", year);
    else if (year%4 == 0)
        printf("%d is a leap year", year);
    else
        printf("%d is not a leap year", year);

    return 0;
}

Output

Enter a year to check if it is a leap year=1900
1900 is not a leap year

Output 2

Enter a year to check if it is a leap year=1600
1600 is a leap year

Output 3

Enter a year to check if it is a leap year=1920
1920 is a leap year

Another Different C Code to Implement Leap Year Algorithm

#include<stdio.h>
int main()
{
    int year;
    printf("Enter a year to check if it is a leap year=");
    scanf("%d",&year);
    if( (year%400==0 || year%100!=0) &&(year%4==0))
      printf("%d is a leap year", year);
    else
      printf("%d is not a leap year", year);
    return 0;
}

Output

Enter a year to check if it is a leap year=1700
1700 is not a leap year

Output 2

Enter a year to check if it is a leap year=1996
1996 is a leap year

Output 3

Enter a year to check if it is a leap year=2008
2008 is a leap year

Enter a year to check if it is a leap year=2019
2019 is not a leap year

Loading

One thought on “C Program Find If Leap Year

Leave a Reply

Your email address will not be published. Required fields are marked *