Q-17.leap_year- Take a year as input and determine whether it is a leap year.

0

#include <stdio.h>
int main()
{
    int year;
    printf("Enter a year: ");
    scanf("%d",&year);
    if(year%4 == 0)
    {
        if( year%100 == 0)
        {
            // year is divisible by 400, hence the year is a leap year
            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;
}




Hints: Follow the steps:
1. if the year is evenly divisible by 4 then it is a leap year, otherwise go to step 2.
2. if the year is evenly divisible by 100 then go to step 3, otherwise it is a leap year.
3. if the year is evenly divisible by 400 then it is leap year, otherwise it is not a leap year.
In simple words we can say, if (year is divisible by 4 AND year is not divisible by 100) OR (year is
divisible by 400) then it is a leap year, otherwise it is not a leap year.

Post a Comment

0Comments
Post a Comment (0)