Left Triangle Example in Java
Introduction
Left Triangle is a classic Java console program that demonstrates the concept with complete source code and sample output. Pattern programs print shapes with nested loops — common in exams and interviews.
This tutorial walks through the program line by line, explains how the logic works, and highlights best practices you can apply in your own code.
Left Triangle Example Program
import java.util.*;
public class LeftTriangle{
public static void main(String[]args){
Scanner in= new Scanner (System.in);
System.out.print("Enter the number of rows: ");
int rows = in.nextInt();
for (int i = 1; i <= rows; i++){
for (int j = 1; j <= i; j++){
System.out.print("*");
}
System.out.println("");
}
}
}Sample Output
Enter the number of rows: 9
*
**
***
****
*****
******
*******
********
*********When to use
Use pattern logic when practicing nested loops or preparing for coding tests that ask for triangle or pyramid output.
How it works
-
Execution begins in the
mainmethod — the JVM calls this method when you run the class. -
import java.util.*;imports a class used later in the program. -
A
Scannerreads typed input from the keyboard (System.in). -
A
println/printcall writes text to the console — part of the sample output below. -
int rows = in.nextInt();updates a variable used in the calculation or output. -
for (int i = 1; i <= rows; i++){updates a variable used in the calculation or output. -
for (int j = 1; j <= i; j++){updates a variable used in the calculation or output. -
A
println/printcall writes text to the console — part of the sample output below.