Index « Previous Next »

Question

Write a program that prompts the user to input a year and determine whether the year is a leap year or not.

Leap Years are any year that can be evenly divided by 4. A year that is evenly divisible by 100 is a leap year only if it is also evenly divisible by 400.

Example :

1992Leap Year
2000Leap Year
1900NOT a Leap Year
1995NOT a Leap Year

Source Code

#include <stdio.h>

int main()
{
    int year;

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

    /* Determine whether the year is leap year */
    if ((year % 4 == 0) && ((year % 400 == 0) || (year % 100 != 0)))
    {
        printf("A leap year");
    }
    else
    {
        printf("Not a leap year");
    }

    return 0;
}

Output

Enter a year :2100
Not a leap year