Java Star (*) Pyramid Examples (Pattern 7,8)


Java Pattern 7

 

 

import java.io.*;

public class JavaPyramidPattern7

{

public static void main(String[] args)

{

for(int i=1; i<= 5 ;i++)

{

for(int j=0; j < i; j++)

{

System.out.print(“*”);

}

System.out.println(“”); //generate a new line

}

                //create second half of pyramid

for(int i=5; i>0 ;i–)

{

for(int j=0; j < i; j++)

{

System.out.print(“*”);

}

System.out.println(“”);  //generate a new line

}

}

}

output:

p7

 

 

 

 

 

 

 

 

 

 

 

 

 

Java Pattern 8

 

import java.io.*;

public class JavaPyramidPattern8

{

public static void main(String[] args)

{

for (int i = 1; i <= 5; i++)

{

for (int s = 5; s > i; s–)

{

System.out.print(” “);  // add spacing

 

}

for (int j = 1; j < i; j++)

{

System.out.print(“*”);  //display/add star

}

for (int j = 1; j < i; j++)

{

System.out.print(“*”);  //display/add star

}

System.out.println(“”);  // add new line

}

for (int i = 1; i <= 5; i++)

{

for (int s = 1; s < i; s++)

{

System.out.print(” “);  // add spacing

}

for (int j = 5; j > i; j–)

{

System.out.print(“*”);  //display/add star

}

for (int j = 5; j > i; j–)

{

System.out.print(“*”);  //display/add star

}

System.out.println(“”);    // add new line

}

}

}

 

output:

p8

 

 

 

 

 

 

 

 

 

 

 

 

 More Example

Java Pyramid pattern 1,2

Java Pyramid Pattern 3,4

Java Pyramid Pattern 5,6

Java Pyramid Pattern 9,10

Comments are closed.