Java Star (*) Pyramid Examples (Pattern1,2)


Java Star (*) Pyramid Examples

This Java Pyramid example shows how to generate star (*)pyramid or triangle like given below using for loop.

Java Pyramid Pattern 1

 /*

Output of the below program would be

*

**

***

****

***** 

*/

import java.io.*;

public class JavaPyramidPattern1

{

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

}

}

}

 output:

p1

 

 

 

 

 

 

 

 

 

 

Java Pattern 2

/*

Output of the below program would be

*****

****

***

**

*

*/

import java.io.*;

public class JavaPyramidPattern2

{

public static void main(String[] args)

{

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:

p2

 

 

 

 

 

 

 

 

 

Java Pyramid Pattern 3,4

fJava Pyramid Pattern 5,6

Java Pyramid Pattern 7,8

Java Pyramid Pattern 9,10


Comments are closed.