C++ Program to Check Leap Year

289
views
Check Leap Year

This program checks whether a year (integer) entered by the user is a leap year or not and displays Check Leap Year.

To understand this Program to Check Leap Year, you should have the knowledge of following C++ programming topics:

  • C++ if, if…else and Nested if…els

All years which are perfectly divisible by 4 are leap years except for century years (years ending with 00) which is the leap year only it is perfectly divisible by 400.

For example, 2012, 2004, 1968 etc are the leap year but, 1971, 2006 etc are not leap year. Similarly, 1200, 1600, 2000, 2400 are leap years but, 1700, 1800, 1900 etc are not.

Program to Check if a year is the leap year or not

#include <stdio.h>

int main()
{
    int year;

    printf("Enter a year: ");
    scanf("%d",&year);

    if(year%4 == 0)
    {
        if( year%100 == 0)
        {
            if ( year%400 == 0)
                printf("%d is a leap year.", year);
            else
                printf("%d is not a leap year.", year);
        }
        else
            printf("%d is a leap year.", year );
    }
    else
        printf("%d is not a leap year.", year);
    
    return 0;
}

Output

Enter a year: 2014
2014 is not a leap year.

In the above C++ program, the user is asked to enter a year and this program checks whether the year entered by a user is the leap year or not, Below is how to do leap year check.

Related Programs

Ask your questions and clarify your/others doubts on checking leap year in C++ by commenting. Documentation