Thread: checking a leap year

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    72

    checking a leap year

    hello everyone
    can i use the statement below to check wether a year is leap or not
    the statement is
    if((year%100== 0 && year%400==0) || (year%100!=0&&year%4==0))
    feb=29;
    else
    feb=28;

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Why don't you test it and find out?

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Yes, something like that should work just fine.

    I assume there is a followup "doesn't work" question coming....
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    72
    excuse me salem what did you mean ?

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    He's expecting the "other shoe to fall" i.e.:

    "I've been trying to get this to work in my assignment, but it just won't give me the right results".


    Personally, I like to enclose compound statements like that if statement, in their own seperate parenthesis, so the bug of operator precedence doesn't bite me too often.


    Adak

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    If the year is divisible by 400, then it's also divisible by 100 so you don't need to check both.

  7. #7
    Registered User
    Join Date
    Jan 2007
    Posts
    72
    do you think just by integrating braces everything is going to be all right

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Yes, the shoe event horizon looms large.

    Basically, you've asked a "meta" question as a disguise of your real question.

    So we just waste a bunch of time pratting about with the obvious until you get around to the real problem which you're facing.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. Problems about gcc installation
    By kevin_cat in forum Linux Programming
    Replies: 4
    Last Post: 08-09-2005, 09:05 AM
  3. Calendar Problem
    By wordup in forum C Programming
    Replies: 7
    Last Post: 10-29-2002, 03:36 PM
  4. error with code
    By duffy in forum C Programming
    Replies: 8
    Last Post: 10-22-2002, 09:45 PM
  5. cant get code to work
    By duffy in forum C Programming
    Replies: 13
    Last Post: 10-20-2002, 05:23 AM