×

Pyramid Program in Java

Pyramid Program in Java

In the previous section, we have discussed about the number pattern programs in Java. The logic for the number pattern and pyramid pattern is the same except for some changes. In this section, we will create pyramid programs in Java.

Standard Pyramid Star Pattern

Filename: PyramidPatternExample.java

public class PyramidPatternExample
{             
public static void main(String argvs[])
{
               int row = 9; // number of rows to be print
               for (int r = 0; r < row; r++) //handling rows
               {
                               for (int ind = row - r; ind > 1; ind--) // handling indentation
                               {
                                              System.out.print(" "); // printing space
                               }
                               for (int c = 0; c <= r; c++) // loop for columns
                               {
                                              System.out.print("* "); // displaying star
                               }             
                               System.out.println(); //after printing each row throws the cursor at new line
               }
}
}

Output:

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

Reversed Pyramid Star Pattern

Filename: PyramidPatternExample1.java

public class PyramidPatternExample1
{             
public static void main(String argvs[])
{
               int rows = 9; // number of rows to print
               for (int r = 0; r <= rows - 1; r++) // loop for handling the rows
               {
                               for (int ind = 0; ind <= r; ind++) // Handling indentation for each row
                               {
                                              System.out.print(" "); // Prints spaces
                               }
                               for (int c = 0; c <= rows - 1 - r; c++) // Handling columns
                               {
                                              System.out.print("*" + " "); // Prints star and spaces
                               }
                               System.out.println(); //after printing each row throws the cursor at new line
               }
}
}

Output:

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

Right Pyramid Star Pattern

Filename: PyramidPatternExample2.java

public class PyramidPatternExample2
{             
public static void main(String argvs[])
{
               int rows = 9; // number of rows to print        
               // logic to print the first half of the pyramid
               for (int r = 0; r <= rows - 1; r++)
               {
                               for (int st = 0; st <= r; st++) // Loop for displaying stars
                               {
                                              System.out.print("*"+ " "); // Prints stars
                               }
                               System.out.println(""); // Ensuring line break at the end of each row
               }
               // logic to print the second half of the pyramid
               for (int r = rows - 1; r >=  0; r--)
               {
                               for(int st = 0; st <= r - 1; st++) // Loop for displaying stars
                               {
                                              System.out.print("*"+ " "); // Prints stars
                               }                                            
                               System.out.println(""); //after printing each row throws the cursor at new line
               }
}
}

Output:

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

Left Pyramid Star Pattern

Filename: PyramidPatternExample3.java

public class PyramidPatternExample3
{             
public static void main(String argvs[])
{
               int rows = 9; // The pyramid will contain 9 rows
                // logic to print first half pyramid
                for (int r = 1; r <= rows; r++) // This loop will handle rows
                {
                               // This inner loop deals with indentation         
                               for (int ind = r; ind < rows; ind++)
                              {
                                              System.out.print(" "); // Printing spaces
                               }
                               for (int c = 1; c <= r; c++) // Handling columns
                               {
                                              System.out.print("*"); // Displaying stars
                               }
                               System.out.println(""); //after printing each row throws the cursor at new line
               }
               // logic to print the second half of pyramid
               for (int r = rows; r >= 1; r--) // loop for rows
                {
                               for(int ind = r; ind <= rows; ind++) // Handling indentation
                               {
                                              System.out.print(" "); // Prints spaces
                               }
                               for(int c = 1; c < r; c++) // Handling columns
                               {
                                              System.out.print("*"); // Prints stars
                               }
                                              System.out.println(""); // Forcing line break
                }
}
}

Output:

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

Related Topics

Java Math atan2() Method

The atan2() method of Math class returns an angle theta from the conversion of rectangular coordinates to polar coordinates. Syntax: public static double atan2(double y, double x) Parameters: The parameter ‘y’ represents the ordinate...

3 minutes read.

Java URL Class with Example

Java URL Uniform Resource Locator To find any resource on the internet, you need to have an address of it. The URL and IP addresses are the pointers used for this purpose....

12 minutes read.

Object Oriented Programming (OOPs)

Since the dawn of earth, we have kept on evolving. The need for evaluation comes with the aim of improving the existing systems when something inappropriate is found for the...

5 minutes read.

The Maximum Rectangular Area in a Histogram in Java

Continuous bars should be used to form the largest possible rectangle. We'll assume in the interest of convenience that each bar's width is 1. Naive Approach In this method, each bar will be...

6 minutes read.

Java String split() method

Java String split() method split current String against given regular expression and returns a char array. Syntax: public String split(String regex)                 public String split(String regex, int...

2 minutes read.

Java Program to find the smallest element in a tree

The variable min is used to store the data of the root, which is initially defined. The smallest node in the left subtree is then located by moving through the...

3 minutes read.

For Loop Program in Java

For Loop Program in Java The for-loop program in Java is written when we want a particular set of statements to be executed repeatedly until the given criteria/ condition is met....

8 minutes read.

Java Abstraction

Java Abstraction Abstraction is an advanced feature of Java to make it transparent. The main motive behind the abstraction is to deal with ideas, not with events. Abstraction is a process...

4 minutes read.

Grepcode Java util date

What is java.util.Date Class? The date and time in Java are provided through the java.util.Date class. If you imported java.util, it could be helpful. Use the Java.util.Date class to implement this class...

4 minutes read.

Java Volatile Keyword

The compiler, runtime, or processors may use any kind of optimization if there aren't any required synchronizations. Although most of the time these improvements are advantageous, they occasionally can result...

6 minutes read.

Java Database Connectivity with MySQL

In this tutorial, we will learn how to connect Database with MySQL in Java. 5 Steps to Connect to the Database in Java Load the driver (or) Register the driver classEstablish a...

4 minutes read.

Untouchable Number in Java

If a number N cannot be divided properly by any positive number, it is said to be an untouchable number. Additionally known as nonaliquot numbers. The sequence is A005114 from...

3 minutes read.

Java Math.multiplyExact() method in Java

Java has an inbuilt math function called Math.multiplyExact() that returns the sum of the parameters. If the result exceeds an integer, an exception is thrown. There is no need to...

2 minutes read.

Magic Square in Java

A square with numbers is referred to as a magic square. The numbers in a magic square of order n are arranged to ensure that the sum of all of...

4 minutes read.

Java Developer

Who is a Java Developer? A Java developer is a skilled programmer who works on commercial applications, software, and webpages.  Java developers can work in two different areas: Operating system development:...

3 minutes read.

How to Create an Object in Java

How to Create an Object in Java An object can be defined as a run time entity that contains the blue printof the class. It means that all the member functions...

5 minutes read.

Java Exception Propagation

Java Exception Propagation When an exception is being thrown from the peak of the stack and not getting caught, it runs down the stack to the previous method, which is sitting...

3 minutes read.

Bean class in Java

The web applications that are created with the help of the JSP or Java Server Pages generally have fairly more functionality than the web pages that specifically are created with...

5 minutes read.

How to Iterate List in Java?

List is a Collection foundation interface in Java. It enables us to keep the collection of objects in order. The four classes that make up the List interface implementation are...

3 minutes read.

How to set path in Java

To make programs that can run on our systems, we need to install programming language-related software in our systems. Different programming languages require different types of software, aka IDEs (Integrated Development...

5 minutes read.