Check for a leap year

pradeep's Avatar author of Check for a leap year
This is an article on Check for a leap year in C.
C program to check whether a year is a leap year or not.

Code: C
/*
** C program to check whether an entered year is a leap year or not
** @author: Pradeep
** @date: 02/12/06
*/


#include<stdio.h>

int main(void)
{
    int year;
   
    printf("Enter the year: ");
    scanf("%d",&year);

    /*
    **    The logic is that the year is either divisible by both
    **    100 and 4 , OR its only divisible by 4 not by hundred
    */

    if(year%400 ==0 || (year%100 != 0 && year%4 == 0))
    {
        printf("Year %d is a leap year",year);
    }
    else
    {
        printf("Year %d is not a leap year",year);
    }
   
    return 0;
}
mayjune, shabbir likes this
Go4Expert Member
2Dec2006,18:28   #2
friendsforniraj's Avatar
i want to know one thing that if yr is divisible by 400 than it will definately be divisible by 100
and if 1896 is leap yr than 1900 will definately be leap yr no checking reqd
so from ma pt. of view hecking divisibilty by 4 is enough
Contributor
2Dec2006,20:13   #3
Aztec's Avatar
Quote:
Originally Posted by friendsforniraj
so from ma pt. of view hecking divisibilty by 4 is enough
No, it's not. Leap year does not always comes after 4 years.
And yes, I did not do any typing mistake.
Team Leader
3Dec2006,01:46   #4
pradeep's Avatar
As per the current Gregorian calendar the determination of the leap year is as follows :
  1. All non-century years divisible by four are leap years.
  2. All century years divisible by 400 is a leap year.
Which means 1900 & 2100 are not a leap years while year 2000 is a leap year.

Read more here..
Go4Expert Member
4Dec2006,15:25   #5
bothie's Avatar
can u provide code which print intreger values given the number in words
Go4Expert Founder
4Dec2006,15:33   #6
shabbir's Avatar
Quote:
Originally Posted by bothie
can u provide code which print intreger values given the number in words
Why dont you try it yourself and when you are stuck you can definitely look for some assistant.
Team Leader
16Dec2006,18:32   #7
pradeep's Avatar
Functions to check for leap year in various languages:

Code: JavaScript
function isLeapyear(year)
   {
       return year%400 ==0 || (year%100 != 0 && year%4 == 0);
   }

Code: PHP
function is_lear_year($year)
   {
       return $year%400 ==0 || ($year%100 != 0 && $year%4 == 0);
   }

Code: Perl
sub{
       $year = shift;
       return 1 if($year%400 ==0 || ($year%100 != 0 && $year%4 == 0));
       # else ;-)
       return 0;
   }

Code: VB
Function IsLeapYear(y)
       If y Mod 400 = 0 Or (y Mod 100 <> 0 And y Mod 4 = 0) Then
           IsLeapYear = True
       Else
           IsLeapYear = False
       End If
   End Function
Ambitious contributor
6Mar2008,13:49   #8
rahul.mca2001's Avatar
we do we need a reminer by 400
Newbie Member
14Aug2009,23:08   #9
kb9snl's Avatar
EXCELLENT post. I hadn't thought to check for leap this way. Saved me a lot of time with this math!

Thanks!
Contributor
25Aug2009,00:41   #10
c_user's Avatar
yup a good and a right program
have a gud day..