SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2008
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help Needed - Only "basic" PHP required.

    Hi All,

    I've just started a class on PHP for my TAFE course and I'm stuck already. We haven't done much PHP as of yet - learnt about if/else statements, switch statements and some loops but that is all. Now I have a task to do involving determining whether a year is a leap year or not. It does not have user input, just declare $year inside the code.

    I've been able to determine whether $year is a leap year when dividing by 4 but am unable to take it more advanced. Part A said to use if/else statements. This is part b (which I'm stuck on):

    If a year is evenly divisible by 4, it is a leap year, unless it is divisible by 100. if it is divisible by 100, it is not a leap year unless it is also evenly divisible by 400. if the year is not a leap year, print the number of years to the next leap year.

    Anyone have any suggestions?

    Thanks.

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    to find the next leap year, when you find one it is simple as adding 4.
    PHP Code:
    var_dump2008 ); 
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Were you taught about the modulus operator? If so, I bet they want to see you use that here.
    Code:
    if (($number % 100) == 0) {
        // evenly divisible by 100
    }

  4. #4
    SitePoint Enthusiast
    Join Date
    Jun 2008
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by crmalibu View Post
    Were you taught about the modulus operator? If so, I bet they want to see you use that here.
    Code:
    if (($number % 100) == 0) {
        // evenly divisible by 100
    }
    yeah we were taught about the modulus operator. I just don't know how to put all of it together.

  5. #5
    SitePoint Addict hiddenpearls's Avatar
    Join Date
    Dec 2007
    Location
    Pakistan
    Posts
    262
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    to determine whether a year is a leap year or not , following are algorithms and pseudo code . hope this gives better understanding !

    if year modulo 400 is 0 then leap
    else if year modulo 100 is 0 then no_leap
    else if year modulo 4 is 0 then leap
    else no_leap

    A more direct algorithm (terms may be grouped either way):

    function isLeapYear (year):
    if ((year modulo 4 is 0) and (year modulo 100 is not 0)) or (year modulo 400 is 0)
    then true
    else false

  6. #6
    SitePoint Enthusiast
    Join Date
    Nov 2007
    Posts
    63
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by hiddenpearls View Post
    to determine whether a year is a leap year or not , following are algorithms and pseudo code . hope this gives better understanding !

    if year modulo 400 is 0 then leap
    else if year modulo 100 is 0 then no_leap
    else if year modulo 4 is 0 then leap
    else no_leap

    A more direct algorithm (terms may be grouped either way):

    function isLeapYear (year):
    if ((year modulo 4 is 0) and (year modulo 100 is not 0)) or (year modulo 400 is 0)
    then true
    else false
    Just want to polish your pseudo-code .Make sure there is odd number of truth condition. btw % is very important operator.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •