Skip to main content
Browse topics

Left Triangle Example in Java

2 min read Updated May 29, 2026
Share

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

java
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

plaintext
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

  1. Execution begins in the main method — the JVM calls this method when you run the class.

  2. import java.util.*; imports a class used later in the program.

  3. A Scanner reads typed input from the keyboard (System.in).

  4. A println / print call writes text to the console — part of the sample output below.

  5. int rows = in.nextInt(); updates a variable used in the calculation or output.

  6. for (int i = 1; i <= rows; i++){ updates a variable used in the calculation or output.

  7. for (int j = 1; j <= i; j++){ updates a variable used in the calculation or output.

  8. A println / print call writes text to the console — part of the sample output below.

Best Practices

Common Mistakes

Frequently Asked Questions

What does the Left Triangle program demonstrate?
It shows how to implement left triangle in Java with a complete runnable example and expected console output.
How do I run this Java program?
Save the code in a `.java` file matching the public class name, compile with `javac`, then run with `java ClassName`.
When would I use this pattern?
Use pattern logic when practicing nested loops or preparing for coding tests that ask for triangle or pyramid output.

Related tutorials

Search tutorials