Java program to print downward triangle star pattern



In this article, we will understand how to print a downward triangle star pattern using Java. In this pattern, the face of the triangle will be in a downward direction, and the base will be in an upward direction. We will learn through two examples: one where the user inputs the number of rows, and another where the number of rows is predefined in the program.

Problem Statement

Write a Java program to print a downward triangle star pattern. Below is a demonstration of the same.

Input: Enter the number of rows : 8 Output: The downward triangle star pattern : * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 

Different ways to print Downward Triangle star pattern

Following are the approaches to print downward triangle star pattern in Java ?

  • Basic iterative approach using multiple for loops.

  • Using Recursion

Using Iterative approach

Below are the steps to print a downward triangle star pattern using user-based input.

  • Import the Scanner class by using java.util package.

  • Create a scanner object to read the number of rows from the user.

  • Prompt the user to input the number of rows (denoted as my_input).

  • Use nested for loop to print spaces and stars.

  • The outer loop (i) runs for each row.

  • The first inner loop(j)prints stars.

  • Display the downward triangle based on user input.

Example

Here, the input is being entered by the user based on a prompt.

import java.util.Scanner; public class DownwardTriangle{ public static void main(String args[]){ int i, j, my_input; Scanner my_scanner = new Scanner(System.in); System.out.print("Enter the number of my_input : "); my_input = my_scanner.nextInt(); System.out.println("The downward triangle star pattern : "); for ( i= 0; i< my_input; i++){ for ( j=0; j< my_input-i; j++){ System.out.print("* "); } System.out.println(); } } }

Output

Enter the number of my_input : 8 The downward triangle star pattern : * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

Using previously defined input

Following are the steps to print a downward triangle star pattern.

  • Define an integer variable (my_input) for the number of rows.

  • Display the number of rows and the downward triangle star pattern on the console.

  • Use nested for loop to print spaces and stars.

  • The outer loop (i) runs for each row.

  • The first inner loop(j)prints stars.

  • Display the downward triangle based on predefined input.

Example

Here, the integer has been previously defined, and its value is accessed and displayed on the console ?

public class DownwardTriangle{ public static void main(String args[]){ int i, j, my_input; my_input = 8; System.out.println("The number of rows is defined as " +my_input); System.out.println("The downward triangle star pattern : "); for ( i= 0; i<my_input; i++){ for ( j=0; j<my_input-i; j++){ System.out.print("* "); } System.out.println(); } } }

Output

The number of rows is defined as 8 The downward triangle star pattern : * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

Using Recursion

Recursion is a process where a method call itself multiple times until it reaches the base condition. To know more about recursion in java you can refer Recursion in java tutorial.

  • Define a recursive method that takes an integer parameter (e.g., n) for the number of stars.

  • Base case: If n == 0 it will stop recursion.

  • If n > 0, use a loop to print stars on the current line. After that print a newline.

  • Invoke recursively all rows.

Example

Here, the integer has been previously defined, and its value is accessed and displayed on the console ?

public class DownwardTriangleRecursion { public static void TrianglePattern(int n) { // Base case if (n == 0) return; // Print stars for the current row for (int i = 0; i < n; i++) { System.out.print("* "); } System.out.println(); // Recursive call for the next row TrianglePattern(n - 1); } public static void main(String[] args) { int rows = 5; System.out.println("The number of rows is defined as 5"); System.out.println("The downward triangle star pattern : "); TrianglePattern(rows); } } 

Output

The number of rows is defined as 5 The downward triangle star pattern : * * * * * * * * * * * * * * * 
Manisha Chand
Manisha Chand

Words That Decode Code

Updated on: 2025-08-22T15:31:15+05:30

903 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements