Q:

C program to check whether a year is leap year or not

belongs to collection: C programs - Miscellaneous C Programs

0

This C program takes an year as input and finds out whether it is a leap year or not. The condition for a year to be leap year is

- A leap year is exactly divisible by 4 except for years ending with 00 (century years).

- A century year is a leap year if it is exactly divisible by 400.

Example of leap years: 1980, 1984, 1988, 1992, 1996, 2000

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

C program to check whether a year is leap year or not - Source code

int main()
{
    int year;
 
    printf("Enter a year: ");
    scanf("%d",&year);
 
    if(year%4 == 0) // check divisibility by 4
    {
        if( year%100 == 0) // check if year is a century year
        {
            if ( year%400 == 0) // check divisibility by 400
                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;
}

Program Output

Case 1:

Enter a year: 2000
2000 is a leap year.

Case 2:

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

Program Explanation

1. As mentioned above, if a year is divisible by 4 but not by 100, then it is a leap year. Also, if a year is divisible by both 4 and by 100, then it is a leap year only if it is also divisible by 400.

2. First an if statement is used to check whether the entered year is divisible by 4. If true, the year is checked for the second condition, otherwise it is printed as non-leap year.(because if a number is not divisible by 4, it will not be divisible by 400).

3. In first nested if statement, the year is checked for divisibility by 100(to check for century year). If it is divisible by 100, it is again checked for divisibility by 400. If true it is a leap year else non leap year.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
The OSI model has ________layers.... >>
<< C program to swap two numbers without using third ...