Star Triangle using For Loops – Java

A simple Java program which outputs nine rows of stars which begin at 1-5 increasing and then 5-1 decreasing using for loops.

public class StarNestedForLoop
{
    public static void main(String[] args)
    {
        final int TIMES =5;

        int countRows, countColumns;

        //For the number of rows produced
        for (countRows=1; countRows<=TIMES; countRows++)
        {
            System.out.println();

            //Creating the number of stars
            for (countColumns=1; countColumns<=countRows; countColumns++)
            {
                System.out.print("* ");

            }//for inner

        }//for outer

        for (countRows=1; countRows<=TIMES; countRows++)
        {
             System.out.println();

             //Creating the number of stars
             for (countColumns=4; countColumns>=countRows; countColumns--)
             {
                   System.out.print("* ");
             }//for inner

        }//for outer

    }//main
}//class

2 thoughts on “Star Triangle using For Loops – Java

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s