[Solved] Printing odd numbers from 1 to 999999 on separate lines using a nested loop [closed]


You can think of the problem as the following:

each progressive line includes the content of the previous line plus another odd number. Therefore, it would make a lot of sense to always store the content of the previous line under a String variable, perhaps.

Now to find every odd number, start with the number 1 and add 2 each time until a given number (in this case 9999999999) is surpassed. The same thing can be done with evens — you simply start with 2 instead of 1.

In a do-while loop structure, it would look something like this (pseudocode):

int lastnum = 1;
String prevLine = lastnum;

do while loop (999999 not passed)
{

    PRINT(prevline + " " + (lastnum + 2));

    lastnum += 2;

    \\update the information stored in the previous line
    prevLine += " " + lastnum;

}

In essence you want to:

1) Start with 1.
2) Print the previous line + the new number (for the first number, the previous line will be blank).
3) Update the previous line
4) Continue steps 2-3 until 999999999 is passed.

11

solved Printing odd numbers from 1 to 999999 on separate lines using a nested loop [closed]