Java Star (*) Pyramid Examples (Pattern3,4)


Java Pattern 3

/*

Output of the below program would be

         *

       **

     ***

   ****

 *****

*/

 

 

 

import java.io.*;

public class JavaPyramidPattern3

{

public static void main(String args[])

{

int i, j, k=8;

for(i=0; i<5; i++)

{

for(j=0; j<k; j++)

{

System.out.print(” “);

}

k = k – 2;

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

{

System.out.print(“* “);

}

System.out.println();

}

}

}

output:

p3

 

 

 

 

 

 

 

 

 

 

Java Pattern 4

 

/*

Output of the below program would be

*****

  ****

    ***

      **

        *

*/

import java.io.*;

public class JavaPyramidPattern4

{

public static void main(String[] args)

{

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

}

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

}

}

}

output:

p4

 

 

 

 

 

 

 

 

 

 

Java Pyramid pattern 1,2

Java Pyramid Pattern 5,6

Java Pyramid Pattern 7,8

Java Pyramid Pattern 9,10

Comments are closed.