Triangle Star Pattern

Program to Print Triangle Star Pattern

PRINTING PATTERN:

*
**
***
****
            

PREREQUISITE:

Basic knowledge of Java language and loops.

ALGORITHM:

1. Take the number of rows as input from the user (height of the triangle) and store it in a variable ('n').
2. Run a loop 'n' times to iterate through the rows (i=1 to i<=n).
3. For each row, print stars equal to the current row number.
4. Move to the next line after each row has been printed.

Code in Java:

import java.util.Scanner;

public class TrianglePattern {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter the number of rows for the triangle:");
        int n = scanner.nextInt();  // Taking input for number of rows

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print("*");  // Printing '*' for each column in the current row
            }
            System.out.println();  // New line after each row
        }

        scanner.close();
    }
}
            

Output:

Enter the number of rows for the triangle:
4
*
**
***
****