Using Java to print upside down right triangle with Astericks for a lab Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Using Java to print upside down right triangle with Astericks for a lab without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I’m currently taking a programming course at my university and I’m stuck on this lab exercise where we have to print this shape:

**********
*********
********
*******
******
*****
****
***
**
*

The code I have prints the shape but it’s right side up and I can’t figure out how to get it in the upside down pattern. here’s the code I have typed up

final int max_rows = 10; //The Shape is 10 rows long

for (int row = 1; row <= max_rows; row++)
{
    for (int star = 1; star <= row; star++)
        System.out.print ("*");
    System.out.println();
}

Answer

Try this:

final int max_rows = 10; //The Shape is 10 rows long

    for (int row = max_rows; row > 0; row--)
    {
       for (int star = 1; star <= row; star++)
          System.out.print ("*");

          System.out.println();
       }
    }

I reversed the first for loop, starting with the max_rows and looping until max_rows is 0.

We are here to answer your question about Using Java to print upside down right triangle with Astericks for a lab - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji