×

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 Boolean logicalOr() Method

The logicalOr() method of Java Boolean class returns the result of implementing logical OR operation on the specified Boolean operands. Syntax: public static boolean logicalOr (boolean a, boolean b) Parameters: The parameters ‘a’ and...

2 minutes read.

How to Split String by Comma in Java

strsplit() technique permits you to break a string given the explicit Java string delimiter. The Java string split property is frequently a space or a comma(,) that you want to...

7 minutes read.

Java vs Golang

Java Java is both a programming language and a platform. Java is a high-level, dependable, object-oriented, and secure programming language. Java was developed in 1995 by Sun Microsystems, which is now an...

4 minutes read.

Transaction Management in java

Definition: A database application is an application that is running against a relational database and executes one or more transactions. A transaction is an executing program that contains some database operations,...

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.

Annotations in Java

Annotations in Java Java Annotations are metadata about the source code. They do not have any direct effect on the execution of the java program. Annotations in Java were introduced in...

4 minutes read.

How to compare characters in Java

In this tutorial, we will learn about how to compare characters in Java. To compare characters in Java, we will learn about what is a character in Java Char The character is...

4 minutes read.

Java Integer shortValue() method

The shortValue() method of Java Integer class returns a short value for this Integer after a narrowing primitive conversion. Syntax public short shortValue () Parameters NA Overrrides: The shortValue () overrides : shortValue in class Number  Return Value This...

1 minute read.

Graph Problems in Java

A graph is a special type of data structure used in programming that is made up of edges connecting each set of finitely many nodes, or vertices, to each other....

14 minutes read.

Best Java Libraries

One of the most widely used programming languages is Java. Java has a large number of libraries, including the standard Java library that includes libraries such as java.lang, java.util, and...

7 minutes read.

Decagonal Numbers in Java

This section explains what is a decagonal number and how to write Java programmes that compute decagonal numbers. Both academics and Java programmer interviews regularly question about the Decagonal number...

3 minutes read.

Differences between Byte Code and Machine Code

This tutorial will discuss the differences between Byte Code and Machine Code. Before that, let's try to know what byte and machine code is. Introduction Every computer has a set of instructions...

4 minutes read.

Ganesha’s Pattern in Java

This part teaches us how to use stars and other special characters to write Ganesha's Pattern in Java programming. Among the most challenging Java pattern applications to code. The Ganesha will be...

3 minutes read.

MVC in Java

A well-known design pattern is Model-View-Controller. The discipline of web development. We can organize our code in this manner. The document stipulates that a program or application must include a...

4 minutes read.

Adapter class in Java

By using the adapter classes, we can implement Listener interfaces. With the help of adapter classes, we can save code as it provides all implementation methods of listener interfaces Advantages of...

3 minutes read.

Java Math nextUp() Method

The nextUp() method of Math class returns the floating-point number adjacent to the argument in direction of the positive infinity. Syntax: public static double nextUp (double d)public static float nextUp (float f) Parameters: The...

2 minutes read.

Applet Program in Java

Applet Program in Java An applet is a program that can be embedded in a web page. Applets programs are run by a web browser. It mainly works on the client-side....

3 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 Comparable Interface

We implement comparable interface when we need a new logic for determining equality. if two objects are equal, the equals() method returns true and compareTo() method returns 0. The basic...

1 minute read.

Java Public Keyword

A Java access modifier is a public keyword. It can be applied to classes, constructors, methods, and variables. It is the type of access modifier that is least constrained. A...

4 minutes read.