Here following approach is used to print Left Triangle Star Pattern with a given number of rows.
- Use Scanner(System.in) class to enter the user input.
- This scanner.nextInt() only take int type input.
- Use first for loop to iterate each row, starting from first row to number of given row.
- Now second for loop to iterate each row and print the space(” “) in decreasing order.
- From third for loop, we print the number of stars in each row is equal to the row number.
import java.util.Scanner;
public class LeftStarPattern {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int numOfRow, i, j, k;
System.out.println("enter the number of rows:- ");
numOfRow = scanner.nextInt();
for (i = 1; i <= numOfRow; i++) {
for (j = (numOfRow - i); j >= 1; j--) {
System.out.print(" ");
}
for (k = 1; k <= i; k++) {
System.out.print("*");
}
System.out.println();
}
scanner.close();
}
}
Output:-
enter the number of rows:
5
*
**
***
****
*****