Thursday, 9 June 2016

Check given year is leap year

A century year is a leap year if it is completely divisible by 400.
A non century year is a leap year if it completely divisible by 4.


Code

#include<stdio.h>

int main()
{
    int yr;

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

    if(yr % 400 == 0 || yr % 100 != 0 && yr % 4 == 0)
        printf("\n Leap year");
    else
        printf("\n Not a leap year");

    return 0;
}


O/P




This can also be done using ternary operator.
(yr % 400 == 0)?printf("Leap year"):(yr % 4 == 0 && yr % 100 != 0)?printf("Leap year"):printf("Not a leap year");

No comments:

Post a Comment