Program to print the star pattern triangle left to right


This program is about to print the star pattern triangle from left to right the row of triangle is defined 5 here in this program. So, only 5 rows will be printed. There are 2 loops will be execute to print the triangle.
There are one integer variables are used in each loop.
We are using for loop the outer loop will execute till the i variable is less then equals to (<=) row which is 5 and i will be incremented by 1 each time and i is initialized from 0.
In the next inner for loop the k variable we are using and initialized each time from 0 when it comes to inner loop from out loop and loop will execute till count of k is less then i so, each time when the inner loop will execute it will print the "* and space" and k will increment from 1.
After completing inner loop a new line will be printed or the cursor will move to the new line.

/******************************************************************************

Program to print the star pattern triangle left to right.

*******************************************************************************/


public class Main
{
                public static void main(String[] args) {
                                int row = 5;
                                for(int i = 0; i <= row; i++){
                                    for(int k = 0; k < i; k++){
                                        System.out.print("* ");
                                    }
                                    System.out.println();
                                }
                }
}


After completing the output is given below:

Output:











If you have any query regarding the program then please ask or comment below.

0 Comments:

Post a Comment