C Program To Check Leap Year


C Program to Find if a given Year is a Leap Year or Not.

Leap Year Logic

Leap Year
1. If a year is a century year(years ending with 00) and if it’s perfectly divisible by 400, then it’s a leap year.
2. If the given year is not a century year and it’s perfectly divisible by 4, then it’s a leap year.

Not Leap Year
1. If the year entered is a century year(perfectly divisible by 100), but not perfectly divisible by 400. Then it’s not a leap year.
2. A year which is not a century year and is not perfectly divisible by 4 is not a leap year.

Leap Year Program: C Programming


[youtube https://www.youtube.com/watch?v=rO4LIxeaD6A]

YouTube Link: https://www.youtube.com/watch?v=rO4LIxeaD6A [Watch the Video In Full Screen.]


In this video tutorial: inside if block we first check if the entered number is a century year. If yes, then we see if it’s perfectly divisible by 400. If true, then its a leap year. If not, its not a leap year.

If the control shifts to else block then the entered year is not a century year. So we check if the year is perfectly divisible by 4. If yes, then it’s a leap year. If not, it’s not a leap year.

Source Code For Leap Year Program: C Programming

 
#include < stdio.h >

int main()
{
    int year;

    printf("Enter the year\n");
    scanf("%d", &year);

    if(year % 100 == 0)
    {
        if(year % 400 == 0)
        {
            printf("%d is leap year!\n", year);
        }
        else
        {
            printf("%d is not leap year!\n", year);
        }
    }
    else
    {
        if(year % 4 == 0)
        {
            printf("%d is leap year!\n", year);
        }
        else
        {
            printf("%d is not leap year!\n", year);
        }
    }

    return 0;
}

Output 1:
Enter the year
2019
2019 is not leap year!

Output 2:
Enter the year
2020
2020 is leap year!

Output 3:
Enter the year
2000
2000 is leap year!

List of some leap years

2000
2004
2008
2012
2016
2020
2024
2028
2032
2036
2040
2044
2048

You can run the program and input any of the above leap year and check for the output.

This program is also a perfect example for nested if else.

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Leave a Reply

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