Answer to Question #172267 in Python for alara demir

Question #172267

1

1 2 1

1 2 4 2 1

1 2 4 8 4 2 1

1 2 4 8 16 8 4 2 1

1 2 4 8 16 32 16 8 4 2 1

1 2 4 8 16 32 64 32 16 8 4 2 1

1 2 4 8 16 32 64 128 64 32 16 8 4 2 1

1 2 4 8 16 32 64 128 256 128 64 32 16 8 4 2 1


By using nested loops, write the code of this triangular shape.


1
Expert's answer
2021-03-17T00:39:16-0400
//pattern1
for(int outer=1;outer<=6;outer++) // outer loop controls number of rows
{
    for(int inner=1;inner<=outer; inner++) // another loop to control number of numbers in each row.
    {
        System.out.print(inner);
    }
    System.out.println(); // move the cursor from the end of the current line to the beggiing to the next line
}

//pattern 2
for(int outer =1; outer<=6 ; outer++) //outer loop controls number of rows
{
    //3-1 create spaces before numbers.
    for(int space=1; space<=6-outer; space++ ) //group controls number of spaces
    {
        System.out.print(" ");
    }

    //3-2 print out real numbers.
    for(int inner=1;inner<=outer; inner++) // another loop to control number of numbers in each row.
    {
        System.out.print(inner);
    }
    System.out.println();
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS