pyramids patterns

Java Code To Print Pyramid and Patterns

We will draw half pyramids, inverted pyramids, full pyramids, inverted full pyramids, Pascal’s triangle, and Floyd’s triangle in Java Programming.

Half Pyramid of *

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

Code

public class PyramidPattern {

	public static void main(String[] args) {
		for(int i=1; i<=5; i++) {
			
			for(int j=1; j<= i; j++) {
				System.out.print("* ");
			}
			
			System.out.println("");
		}
	}
	
}

Half Pyramid of Numbers

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

Half Pyramid of Alphabets

A
B B
C C C
D D D D
E E E E E

Inverted half pyramid of *

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

Inverted half pyramid of numbers

1 2 3 4 5
1 2 3 4 
1 2 3
1 2
1

Full Pyramid of *

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

Code
public class PyramidPattern2 {

	public static void main(String[] args) {
		for (int i = 1; i <= 5; i++) {
			
			// in pattern 1 triangle add this for center alignment 
            for (int j = 1; j <= 5 - i; j++) {
                System.out.print(" ");
            }
            
            
            for (int k = 1; k <= i; k++) {
                System.out.print("* ");
                // System.out.print(k + " "); //number print
            }
            System.out.println();
        }
	}
	
}

Full Pyramid of Numbers

        1
      2 3 2
    3 4 5 4 3
  4 5 6 7 6 5 4
5 6 7 8 9 8 7 6 5

Inverted full pyramid of *

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

Pascal’s Triangle

           1
         1   1
       1   2   1
     1   3   3    1
   1  4    6   4   1
 1  5   10   10  5   1

Floyd’s Triangle

1
2 3
4 5 6
7 8 9 10

Similar Posts