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 Reverse Pyramid Star Pattern

By Chaitanya Singh | Filed Under: Java Examples

In this article, you will learn how to write a java program to print reverse Pyramid Star Pattern.

This is how a reverse pyramid pattern using stars looks:

Number of rows = 7

Output:

* * * * * * * *
 * * * * * * *
  * * * * * *
   * * * * *
    * * * *
     * * *
      * *
       *

Example 1: Program to Print Reverse Pyramid Pattern

public class JavaExample
{
  public static void main(String[] args)
  {
    int numberOfRows=7;
    //This loop runs based on the number of rows
    //In this case, the loop runs 7 times to print 7 rows
    for (int i= 0; i<= numberOfRows-1; i++)
    {

      //This loop prints starting spaces for each row of pattern
      for (int j=0; j<=i; j++)
      {
        System.out.print(" ");
      }
      //This loop prints stars and the space between stars for each row
      for (int k=0; k<=numberOfRows-1-i; k++)
      {
        System.out.print("*" + " ");
      }
      //To move the cursor to new line after each row
      System.out.println();
    }
  }
}  

Output:

Print Reverse Pyramid Star Pattern

Example 2: Program to Print Reverse Pyramid Pattern based on user input

In the following program, the number of rows is entered by the user and the program gets that value using Scanner. The value then used by for loop to print the pattern consisting user entered number of rows.

import java.util.Scanner;
public class JavaExample
{
  public static void main(String[] args)
  {
    int numberOfRows;
    System.out.println("Enter Number of rows: ");
    //Getting the number of rows input from user
    Scanner sc = new Scanner(System.in);
    numberOfRows = sc.nextInt();
    sc.close();

    //This loop will run based on the input by user
    //if user enters 5 then the loop will run 5 times
    //if user enters 8 then the loop will run 8 times
    for (int i= 0; i<= numberOfRows-1; i++)
    {

      //Print spaces before first star of each row
      for (int j=0; j<=i; j++)
      {
        System.out.print(" ");
      }
      //Print stars and spaces between them
      for (int k=0; k<=numberOfRows-1-i; k++)
      {
        System.out.print("*" + " ");
      }
      //new line for each row
      System.out.println();
    }
  }
}  

Output: User enters number 8 so the program prints 8 rows reverse pyramid pattern.

Print Reverse Pyramid pattern based on user input

Related Java Programs

  1. Java Program to print Downward Triangle Star Pattern
  2. Java Program to print Diamond Pattern
  3. Java Program to print Pyramid 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