Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Leap Year code 99% right but

Options
  • 01-09-2013 10:03pm
    #1
    Registered Users Posts: 34,044 ✭✭✭✭


    Here is a code I have in place for sorting out Leap Years. It Works for every year bar 1700, 1800, 1900. Works for 1600 and 2000. Basically any years bar start of decades not ending in 4s

    Can anyone help
    if((year % 4 == 0 && year % 100 != 0) || year % 400==0) {
    return true;
    }

    else{
    return false;
    }


    }


Comments

  • Registered Users Posts: 1,311 ✭✭✭Procasinator


    1700, 1800, 1900 were not leap years. So when you say it doesn't work, do you mean it reports them years as leap ones?


  • Registered Users Posts: 291 ✭✭Seridisand


    if((year % 4 == 0 && year % 100 != 0) || year % 400==0) {
                return true;
            }
           
            else{
                return false;
            }
            
            
        }
    
    This evaluates to year divide by 4/remainder =0, AND year divide by 100/remainder NOT = 0 OR year divide by 400/remainder = 0:

    1700 / 4 = 0 True (Remainder 0)
    1700 /100 NOT = 0 False (Remainder 0)
    1700 / 400 = 0 False (Remainder 100)
    (True AND False) OR False == ALWAYS FALSE

    1800 / 4 = 0 True (Remainder 0)
    1800 /100 NOT = 0 False (Remainder 0)
    1800 / 400 = 0 False (Remainder 200)
    (True AND False) OR False == ALWAYS FALSE


  • Registered Users Posts: 2,021 ✭✭✭ChRoMe


    What language is this? I suppose what I'm really asking is why are you doing this by hand?


  • Moderators, Society & Culture Moderators Posts: 17,642 Mod ✭✭✭✭Graham


    ChRoMe wrote: »
    I suppose what I'm really asking is why are you doing this by hand?

    I would hazard a guess that it's homework. It doesn't appear to be the usual 'please complete my entire assignment' type of question though which is probably a good sign.


  • Registered Users Posts: 16,402 ✭✭✭✭Trojan


    ChRoMe wrote: »
    What language is this?

    Looks like C to me.

    It seems a bit early in the academic year for homework like this, but has all the hallmarks.

    Fair play to OP for not doing the usual "do it for me" plea.


  • Advertisement
  • Moderators, Society & Culture Moderators Posts: 17,642 Mod ✭✭✭✭Graham


    Level 5 City and Guilds exam prep??

    Trojan wrote: »
    Fair play to OP for not doing the usual "do it for me" plea.

    +1


  • Registered Users Posts: 1,311 ✭✭✭Procasinator


    Trojan wrote: »
    Looks like C to me.

    It seems a bit early in the academic year for homework like this, but has all the hallmarks.

    Fair play to OP for not doing the usual "do it for me" plea.

    It's a C based language anyhow, but it could easily be a valid fragment from Javascript, Java, C#, C++, e.t.c.


  • Registered Users Posts: 2,021 ✭✭✭ChRoMe


    It's a C based language anyhow, but it could easily be a valid fragment from Javascript, Java, C#, C++, e.t.c.

    Yeah saying it looks like C based syntax really doesn't help narrow it down!

    If its homework fair enough, however if its not, you really should be using a proper time lib (such as Joda for Java) instead of calculating this stuff by hand.


  • Moderators, Society & Culture Moderators Posts: 17,642 Mod ✭✭✭✭Graham


    I think most of the OP's recent dev posts have been Java related.


  • Registered Users Posts: 34,044 ✭✭✭✭The_Kew_Tour


    I don't want it done. Its a past exam paper one thankfully I wont be doing again.

    I was just asking for help that is all.


  • Advertisement
  • Moderators, Society & Culture Moderators Posts: 17,642 Mod ✭✭✭✭Graham


    I don't want it done. Its a past exam paper one thankfully I wont be doing again.

    I was just asking for help that is all.

    I think that had been established in your absence TKT :D


  • Registered Users Posts: 7,500 ✭✭✭BrokenArrows


    It all seems to work fine for me.

    Can you explain further as to why you think your code is not working?


    Spoiler below is how .NET's IsLeapYear(int year) method works.
    if ((year % 4) != 0)
    {
    return false;
    }
    if ((year % 100) == 0)
    {
    return ((year % 400) == 0);
    }
    return true;


  • Registered Users Posts: 34,044 ✭✭✭✭The_Kew_Tour


    its actually working fine for me now. Thanks guys. it was me that was wrong in first place.


Advertisement