Results 1 to 4 of 4
Like Tree2Likes
  • 1 Post By jim829
  • 1 Post By DarrylBurke

Thread: for, do, print question

  1. #1
    rhym1n is offline Member
    Join Date
    Feb 2013
    Posts
    32
    Rep Power
    0

    Default for, do, print question

    Consider the following program segment (written in pseudocode):

    for i := 1 to 123 do
    for j := 1 to i do
    print i*j


    How many times is the print statement of the third line executed?

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,909
    Rep Power
    5

    Default Re: for, do, print question

    Well, the first time thru is 1, the second time thru is 2. So far 1 + 2 = 3. Then third time thru is 3. So 1 + 2 + 3 = 6.
    So summing the numbers from 1 to n is equal to (n)(n+1)/2 where in this case, n = 123. The answer should be 7626.

    Regards,
    Jim
    pishu likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    rhym1n is offline Member
    Join Date
    Feb 2013
    Posts
    32
    Rep Power
    0

    Default Re: for, do, print question

    Thank you Jim.

    Is that a large spam post by spyphone? lol

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

    Default Re: for, do, print question

    Quote Originally Posted by rhym1n View Post
    Is that a large spam post by spyphone? lol
    Please don't comment on or respond to spam, as your post goes out of context when the spam is removed.

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

Similar Threads

  1. Replies: 6
    Last Post: 01-07-2013, 03:48 PM
  2. Replies: 2
    Last Post: 05-25-2012, 05:00 PM
  3. Javax Print Attribute for Selection Print Range
    By rsawatzky in forum AWT / Swing
    Replies: 0
    Last Post: 04-26-2012, 01:14 AM
  4. Replies: 8
    Last Post: 01-21-2012, 02:14 AM
  5. Print the text file and print preview them
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-23-2008, 12:04 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
  •