problem with if statements (leap year)

This is a discussion on problem with if statements (leap year) within the C++ Programming forums, part of the General Programming Boards category; how would you write an if statement to determine whether or not it is a leap year. A year is ...

  1. #1
    The code loser
    Join Date
    Mar 2007
    Posts
    13

    problem with if statements (leap year)

    how would you write an if statement to determine whether or not it is a leap year. A year is a leap year if it is evenly divisible by four hundred or if it is evenly divisible by four, but not by one hundred

    i tried to start it like so
    Code:
    if (((((year/400)%2)== 0)||((((year/4)%2)== 0)&&(((year/100)%2)!= 0)))&&((month==2)&&(day==29)))

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Code:
    //if year is evenly divisible by 400 OR year is evenly divisible by 4 AND year is not evenly divisible by 100
    
    if(year%400 == 0 || year%4 == 0 && !(year%100 == 0))
    Last edited by The Brain; 03-30-2007 at 06:58 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by The Brain View Post
    Code:
    //if year is evenly divisible by 400 OR year is evenly divisible by 4 AND year is not evenly divisible by 100
    
    if(year%400 == 0 || year%4 == 0 && !(year%100 == 0))
    You might want to write this as
    Code:
    if (year%400 == 0 || (year%4 == 0 && year%100 != 0))
    since not everyone knows off the top of their head that && has higher precedence than || (I had to look it up).

    Edit: If efficiency matters, you can also rewrite it as
    Code:
    if ((year%4 == 0 && year%100 != 0) || year%400 == 0)
    since the first condition is much more likely to be true, allowing the || shortcutting to skip the second condition. The two conditions for && should be left as is, since the first one is much more likely to be false, allowing the && shortcutting to skip the second one.
    Last edited by robatino; 03-30-2007 at 07:12 PM.

  4. #4
    The code loser
    Join Date
    Mar 2007
    Posts
    13
    thnx. i think i was thinking too hard

  5. #5
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    Unhappy

    Quote Originally Posted by radiantarchon28 View Post
    thnx. i think i was thinking too hard
    I feel the same way when it comes to women
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    since not everyone knows off the top of their head that && has higher precedence than || (I had to look it up).
    GCC actually gives a warning about this is you pass it -W -Wall. (That is, || and &&s without parentheses.)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why does this program say 1700 is a leap year?
    By newbcore in forum C Programming
    Replies: 7
    Last Post: 12-19-2008, 01:03 AM
  2. Calendar Problem
    By wordup in forum C Programming
    Replies: 7
    Last Post: 10-29-2002, 02:36 PM
  3. debug program
    By new_c in forum C Programming
    Replies: 3
    Last Post: 03-18-2002, 10:50 PM
  4. Simplified code
    By soonerfan in forum C Programming
    Replies: 2
    Last Post: 12-05-2001, 02:50 PM
  5. Help Me Out!! Pls
    By Joanna in forum C++ Programming
    Replies: 5
    Last Post: 10-27-2001, 05:08 AM

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