0 votes
in Design Patterns by
Write a Java Program to display the left triangle star pattern on the system console.

1 Answer

0 votes
by
This can be again be achieved by using nested loops and calculatingly adding spaces and stars as shown in the logic below:

public class InterviewBitLeftPyramid{

   public static void printLeftTriangleStars(int n) {

       int j;  

       for(int i=0; i<n; i++){  // outer loop for number of rows(n)

           for(j=2*(n-i); j>=0; j--){   // for spaces

               System.out.print(" "); // to print space

           }

           for(j=0; j<=i; j++){ // for columns

               System.out.print("* "); // Print star and give space

           }           

           System.out.println(); // Go to next line after every row

       }

   }

   public static void main(String args[]){  

       printLeftTriangleStars(5); //print stars of 5 rows in left triangle fashion

   }

}

Output:

          *

        * *

      * * *

    * * * *

  * * * * *

Related questions

+1 vote
asked May 17, 2020 in Python by sharadyadav1986
+1 vote
asked Oct 17, 2019 in Design Patterns by Robin
...