1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Check for a leap year

Discussion in 'C' started by pradeep, Dec 1, 2006.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    C program to check whether a year is a leap year or not.

    Code:
    /*
    ** 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;
    }
     
  2. friendsforniraj

    friendsforniraj New Member

    Joined:
    Nov 24, 2006
    Messages:
    40
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    studying
    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
     
  3. Aztec

    Aztec New Member

    Joined:
    May 9, 2006
    Messages:
    90
    Likes Received:
    0
    Trophy Points:
    0
    No, it's not. Leap year does not always comes after 4 years.
    And yes, I did not do any typing mistake.
     
  4. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    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..
     
  5. bothie

    bothie New Member

    Joined:
    Nov 14, 2006
    Messages:
    24
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    student
    Location:
    Harare,Zimbabwe
    can u provide code which print intreger values given the number in words
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,287
    Likes Received:
    364
    Trophy Points:
    83
    Why dont you try it yourself and when you are stuck you can definitely look for some assistant.
     
  7. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    Functions to check for leap year in various languages:

    Code:
    function isLeapyear(year)
       {
           return year%400 ==0 || (year%100 != 0 && year%4 == 0);
       }
    PHP:
    function is_lear_year($year)
       {
           return 
    $year%400 ==|| ($year%100 != && $year%== 0);
       }
       
    Code:
    sub{
           $year = shift;
           return 1 if($year%400 ==0 || ($year%100 != 0 && $year%4 == 0));
           # else ;-)
           return 0;
       }
       
    Code:
    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
     
  8. rahul.mca2001

    rahul.mca2001 New Member

    Joined:
    Feb 13, 2008
    Messages:
    103
    Likes Received:
    0
    Trophy Points:
    0
    we do we need a reminer by 400
     
  9. kb9snl

    kb9snl New Member

    Joined:
    Aug 14, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    EXCELLENT post. I hadn't thought to check for leap this way. Saved me a lot of time with this math!

    Thanks!
     
  10. c_user

    c_user New Member

    Joined:
    Aug 23, 2009
    Messages:
    86
    Likes Received:
    8
    Trophy Points:
    0
    Occupation:
    Php dev
    Location:
    Bhubaneswar
    yup a good and a right program
    have a gud day..
     
  11. manoj1987

    manoj1987 New Member

    Joined:
    Apr 23, 2009
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    student
    Location:
    Chennai
    Home Page:
  12. Deucel

    Deucel New Member

    Joined:
    Feb 14, 2011
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Washington State
    Below is some code I wrote w/ pradeep's help. It was an assignment in a book I'm going through.

    ---------------------------------------------------------------------------------------------
    Code:
    #include<stdio.h>
    main()
    {
        int month, year, n, leap, notLeap;
    
    
    
        printf("\n\nEnter a month and year:   ");
        scanf("%d %d", &month, &year);
    
    
    
        if(year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))
    
            year = leap;
    
        else
            year = notLeap;
    
        if (year == leap && month == 2)
            n = 29;
    
        if (year == notLeap && month == 2)
            n = 28;
    
        if (month == 1) n = 31;
        if (month == 3) n = 31;
        if (month == 4) n = 30;
        if (month == 5) n = 31;
        if (month == 6) n = 30;
        if (month == 7) n = 31;
        if (month == 8) n = 31;
        if (month == 9) n = 30;
        if (month == 10) n = 31;
        if (month == 11) n = 30;
        if (month == 12) n = 31;
    
        printf("\n\nThere are %d days in that month.", n);
    
    
        getch();
        return;
    }
     
    Last edited by a moderator: Feb 14, 2011
  13. Plastech

    Plastech New Member

    Joined:
    Nov 21, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Your code is good but your logic isn't. Specifically, it's not just that the number is divisible by 100 and 4, it's that the number is divisible by 400. 200 is divisible by 100 and 4, but isn't a leap year.

    To say that a number is divisible by 100 and 4, or 100 or 4, is that same as saying a number is divisible by 4.

    That is: (A ^ B) v (A ^ !B) = A ^ (B v !B) = A.

    This is the same logic that makes all those ads so hilarious: "You can make up to $50,000 a year, or more!" So I guess the only amount you can't make is $50,000!

    Sorry, anal logician here ;)

    -Plast
     

Share This Page