checking a leap year

This is a discussion on checking a leap year within the C Programming forums, part of the General Programming Boards category; hello everyone can i use the statement below to check wether a year is leap or not the statement is ...

  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,211
    Why don't you test it and find out?

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,541
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,541
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 02: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21