
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java program to print left triangle star pattern
In this article, we will understand how to print the left triangle star pattern in Java. This pattern is also known as the Mirrored Right Triangle Star pattern. Below is a demonstration of the same-
Input: Number of rows: 6 Output: * * * * * * * * * * * * * * * * * * * * *
Printing Left Triangle Star Pattern
Following are the different approaches to print the left triangle star pattern -
-
Using an Iterative Approach
-
Using Recursion
Using an Iterative Approach
In this approach, we will use a nested for loop.
-
Declare a variable, rows, to define the total number of rows for the triangle.
-
Use an outer for loop with variable i (from 1 to rows) to represent the current row number. Each iteration of i prints one line of the pattern.
-
Use a nested for loop inside the outer loop with variable j. This loop runs from 1 to rows - i to print spaces. These spaces shift the stars to the right, creating the left triangle alignment.
-
Use another nested or inner loop with variable k (from 1 to i) to print the stars (*). The number of stars increases with each row.
-
After both nested loops, move to the next line.
Example
The following program prints a left triangle star pattern using nested loops.
public class LeftTrianglePattern { public static void main(String[] args) { int rows = 6; for (int i = 1; i <= rows; i++) { // Print spaces for (int j = 1; j <= rows - i; j++) { System.out.print(" "); } // Print stars for (int k = 1; k <= i; k++) { System.out.print("* "); } // Move to next line System.out.println(); } } }
Following is the output of the above program -
* * * * * * * * * * * * * * * * * * * * *
Using Recursion
In Java, Recursion is a process in which a function calls itself multiple times until it reaches a base condition, and is called recursion and the corresponding function is called a recursive function.
-
Declare a variable, rows, to define the total number of rows in the triangle pattern.
-
Use the recursive method newRow(n, totalRows) to represent the outer loop, where n is the current row being printed.
-
The base condition for newRow is if n == 0, that means no more rows to print, so recursion stops.
-
Inside newRow, recursively call newRow(n - 1, totalRows) to print previous rows before printing the current row.
-
Call the recursive method printRow(n, totalCols) to print each column in the current row. It prints a space if totalCols > n, otherwise it prints a star.
-
Base condition for PrintRows is if totalCols == 0, stops the column printing. After each row is printed, a new line is added using System.out.println().
Example
The following program prints a left triangle star pattern using recursion.
public class LeftTrianglePattern { // Recursive function to print characters public static void printRow(int n, int totalCols) { if (totalCols == 0) { return; } // Print space until the stars begin if (totalCols > n) { System.out.print(" "); } else { System.out.print(" *"); } // Move to next column printRow(n, totalCols - 1); } // Recursive function to print each row public static void newRow(int n, int totalRows) { if (n == 0) { return; } // Print rows from top to bottom newRow(n - 1, totalRows); printRow(n, totalRows); System.out.println(); } public static void main(String[] args) { int rows = 5; newRow(rows, rows); } }
Following is the output of the above program -
* * * * * * * * * * * * * * *