BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Program to Print Downward Triangle Star Pattern

By Chaitanya Singh | Filed Under: Java Examples

In this article, you will learn how to print downward triangle star pattern in Java.

This is how a downward triangle star pattern looks:

number of rows = 6

Output:
* * * * * *
* * * * *
* * * *
* * *
* *
*

Example 1: Program to print downward triangle star pattern

public class JavaExample {
  public static void main(String[] args)
  {
    int numberOfRows = 7;
    for (int i = numberOfRows - 1; i >= 0; i--) {
      for (int j = 0; j <= i; j++) {
        System.out.print("*");
      }

      //New line for new row
      System.out.println();
    }
  }
}

Output:

Print Downward Triangle Star Pattern

Example 2: Print downward triangle star pattern based on user input

In this example, program prints the downward triangle star pattern based on the number of rows entered by user. The logic is same as above example, except here the numberOfRows is not hardcorded and its value is captured by using

import java.util.Scanner;
public class JavaExample {
  public static void main(String[] args)
  {
    int numberOfRows;
    System.out.println("Enter number of rows: ");
    Scanner sc = new Scanner(System.in);
    numberOfRows = sc.nextInt();
    for (int i = numberOfRows-1; i >= 0; i--) {
      for (int j = 0; j <= i; j++) {
        System.out.print("*");
      }

      //New line for new row
      System.out.println();
    }
  }
}

Output:

Print downward triangle star pattern based on user input

Related Java Programs

  1. Java Program to print Diamond Pattern
  2. Java Program to print Pyramid Star Pattern
  3. Java Program to print Left Triangle Star Pattern
  4. Java Program to print Right Triangle Star Pattern
❮ Learn JavaJava Examples ❯

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Programs

  • C Programs
  • Java Programs
  • C++ Programs

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap