Results 1 to 9 of 9
  1. #1
    tonie is offline Member
    Join Date
    Jun 2013
    Posts
    16
    Blog Entries
    1
    Rep Power
    0

    Default What's the logic behind this code?

    I know how to write this code but don't understand the logic how it comes to "Product = 24". Would greatly appreciate it if someone could write out the steps.

    Java Code:
    int product = 1;
       int i = 1;
       while ( i < 5 )
       {
            product = product * i;
            i = i + 1; 
       }
       System.out.println( “product = “ + product );

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: What's the logic behind this code?

    Quote Originally Posted by tonie View Post
    I know how to write this code but don't understand the logic
    The how did you write it?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    tonie is offline Member
    Join Date
    Jun 2013
    Posts
    16
    Blog Entries
    1
    Rep Power
    0

    Default Re: What's the logic behind this code?

    I just know it's 1*2*3*4, but I don't know how to interpret it or explain it.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: What's the logic behind this code?

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    tonie is offline Member
    Join Date
    Jun 2013
    Posts
    16
    Blog Entries
    1
    Rep Power
    0

    Default Re: What's the logic behind this code?

    Didn't read the above but this is what I got in excel when I typed it out. I hope it's right.

    Pro = 1 * 1 = 1-----I = 1 + 1 = 2
    Pro = 1 * 2 = 2-----I = 2 + 1 = 3
    Pro = 2 * 3 = 6-----I = 3 + 1 = 4
    Pro = 6 * 4 = 24----I = 1 + 1 = 5 <-- stop at 5.

    Am I right?

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default Re: What's the logic behind this code?

    Quote Originally Posted by tonie View Post
    Am I right?
    Yep, you're right; simply follow the algorithm step by step and you get 24 as the end result.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    tonie is offline Member
    Join Date
    Jun 2013
    Posts
    16
    Blog Entries
    1
    Rep Power
    0

    Default Re: What's the logic behind this code?

    Thank you for confirming my answer, Jos. Very much appreciated.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: What's the logic behind this code?

    Excel?

    Didn't you have any paper handy?

  9. #9
    tonie is offline Member
    Join Date
    Jun 2013
    Posts
    16
    Blog Entries
    1
    Rep Power
    0

    Default Re: What's the logic behind this code?

    LOL... I do. I was doing it on paper, but I decided to do it on excel because I needed to copy and paste it here.

Similar Threads

  1. Where's the logic?
    By diamonddragon in forum New To Java
    Replies: 11
    Last Post: 01-28-2012, 05:16 AM
  2. Logic Help
    By Barbados in forum New To Java
    Replies: 6
    Last Post: 01-23-2012, 12:24 AM
  3. Need help in logic
    By nn12 in forum New To Java
    Replies: 3
    Last Post: 03-23-2011, 07:44 PM
  4. Need help on logic
    By nn12 in forum New To Java
    Replies: 6
    Last Post: 03-10-2011, 12:06 PM
  5. Cant get the logic right
    By jermaindefoe in forum New To Java
    Replies: 4
    Last Post: 03-11-2008, 01:22 AM

Posting Permissions

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