×

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

Hogben Numbers in Java

In this section, we will discover what the Hogben number is and develop Java programs that compute it. Java coding interviews and academic exams typically involve questions about the Hogben...

3 minutes read.

Java Program to print even and odd numbers using 2 threads

Using two threads in a single thread is even odd printing in Java programming with multiple threads. To create code that prints even and odd using two threads, we must...

2 minutes read.

PriorityBlockingQueue Class in Java

What is the Queue? An abstract data structure like Stacks is a queue. A queue is open on both ends. Data is always pushed to one end, called enqueue, and removed...

4 minutes read.

String Array in Java

String Array in Java An array is alinear data structure that stores similar type of data. It allows us to store fixed number of elements.It can be of different data types...

6 minutes read.

Static vs Non-Static in Java

A static method can access and update the value of a static data member. The static keyword is mostly used in Java for memory management. The static keyword can be...

6 minutes read.

Operators in Java

There is a good operator environment provided by Java. These operators are divided into four different groups i.e. arithmetic, bitwise, relational, and logical. There are other operators also available to...

7 minutes read.

Java Integer parseUnsignedInt() method

The parseUnsignedInt() method of Java Integer class parses the string argument as an unsigned decimal integer. The second parameter parses the string argument as an unsigned integer in the radix specified...

2 minutes read.

Java Font

The font is a Java class that is a part of java.awt package. The Serializable interface is implemented by it. The direct recognized child of a Java Font class is...

8 minutes read.

Print Pencil Shape Pattern in Java

Another pattern made from asterisk symbols that use loops and other logical concepts is the pencil pattern. It is usually requested to draw a pattern using a program. To write the...

6 minutes read.

Java Read File Line by line

Java provides two options to read files line by line. Each option offers different benefits and has its own set of drawbacks. Following are the ways through which on accomplish...

5 minutes read.

Java Long Keyword

Long is a primitive data type in Java. To initilize variables, we implement the long keyword. It can also be applied to techniques. A 64-bit two's complement integer can put...

3 minutes read.

Java Integer toOctalString() method

The toOctalString() method of Java Integer class returns a string representing the specified int argument as an unsigned integer in base 8. Syntax public static String toOctalString (int  i) Parameters The parameter ‘i’ represents...

1 minute read.

Java inheritance with Example

Java inheritance Java inheritance is a mechanism in which a child object acquires all the properties and behaviors of a parent object. It helps in reusing the code and establishes...

7 minutes read.

XOR of Array Elements Except Itself in Java

A lot of the biggest IT organisations, including The Google, Amazon, TCS, and The Accenture, etc., question about this issue in their interview processes. By addressing the issue, one hopes to assess...

5 minutes read.

Multiple Inheritance Programs in Java

A component of the object-oriented notion known as multiple inheritances allows a class to inherit properties from multiple parent classes. When methods that have the same signature are present in...

4 minutes read.

Java Interface Lock

A synchronisation method called the Lock interface is available as of JDK 1.5. It is comparable to a synchronised block but more complex and versatile. The package java.util.concurrent contains the...

4 minutes read.

Sorting Algorithms in Java

Sorting Algorithms in Java Sorting is the technique that puts the elements of an array or list either in descending or ascending order. For example, take an array A, whose elements...

3 minutes read.

Read JSON file in Java

 To know about reading a JSON file in Java first we must know about the JSON file. JSON: JSON is “JavaScript Object Notation”. The JSON is the simple format for storing and...

3 minutes read.

If Condition in Lambda Expression Java

The new and significant lambda expression feature of Java was added in Java SE 8. It provides a clear and concise mechanism for describing a single-method interface using an expression....

4 minutes read.

JDBC Program in Java

JDBC Program in Java JDBC is an API that defines how a client may access a database. It is a part of Java Standard Edition (Java SE). JDBC stands for Java...

4 minutes read.