Create pyramid output using nested loops

Asked By 0 points N/A Posted on -
qa-featured

 

Hello! I want to create a PYRAMID of digits using LOOPS(nested). But I don’t know how to use nested loops. As I always receive an ERROR. Anybody will please help in designing this code. And also tell me the rules how simple loops differ from nested loops

                                                      1

                                                    232

  34543

4567654

              567898765

            67890109876

                                          7890123210987

         890123454321098

        90123456765432109

      0123456789876543210

SHARE
Answered By 5 points N/A #85766

Create pyramid output using nested loops

qa-featured

Nested loops are basically a loop in a loop.  It is kind of like circuit training done by athletes.  They run to one station do a bunch of repeated actions go to the next station and do another bunch of repeated actions until they reach the last station and start all over again.

So in the case of nested loops, the outer loop triggers the inner loop to do a bunch of things.  When the inner loop finishes, it exits, and the outer loop once again trigger it.  This carries on until the outer loop's conditions is satisfied.

When coding nested statements, it is very important to practice good indentation.  With good indentation, you will have a good idea in which nest you are working on.

For every programming problem, it is just a collection of small problems.  The difficulty is identifying the small problems.  In this case, you will have to build a pyramid of 10 levels (1st problem).  In each level, the 1st and last number will be one up of the previous level's 1st number(2nd problem).  And the level have to be populated by increasing number from both ends till the center of the line(3rd problem).

So in this case we are looking at a loop in a loop.  The outer loop will handle the building of the levels, while the inner loop will handle the populating of the level.

If one will to notice, the size of the level is also 2 more then the previous level.  Which will mean the center position of the level is always (Size of previous level + 2) / 2 + 1.

So here is the pseudo-code for this problem:

 

Loop ( level > 10 ) {
current start = Previous level start + 1
Loop ( end of level? ) {
is more then center
current–;
if no
current++;
}
}
 
Of course this is just an extremely simplified version.  You will have to convert it into actual code.
 

Related Questions