Java Star (*) Pyramid Examples


Java Star (*) Pyramid Examples

 

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

 

 

 

 

Java 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 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 Pattern 5

 

/*

Output of the below program would be

*

***

*****

*******

*********

*/

import java.io.*;

public class JavaPyramidPattern5

{

public static void main(String args[])

{

int i, j, k=1;

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

{

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

{

System.out.print(“* “);

}

k = k + 2;

System.out.println();

}

}

}

output:

p5

 

 

 

 

 

 

 

 

 

Java Pattern 6

 

/*

Output of the below program would be

               *

           ***

       *****

   *******

*********

*/

 

import java.io.*;

public class JavaPyramidPattern6

{

public static void main(String args[])

{

int i, j, k=16, tim=1;

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

{

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

{

System.out.print(” “);

}

k = k – 4;

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

{

System.out.print(“* “);

}

tim = tim + 2;

System.out.println();

}

}

}

p6

 

 

 

 

 

 

 

 

 

 

 

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

 

 

 

 

 

 

 

 

 

 

 

 

 

Java pattern 9

import java.io.*;

public class JavaPyramidPattern9

{

public static void main(String[] args)

{

                //generate upper half of the pyramid

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

{

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

{

System.out.print(“*”);

}

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

}

                //generate bottom half of the pyramid

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

{

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

{

System.out.print(“*”);

}

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

}

}

}

 

 output:

p9

 

 

 

 

 

 

 

 

 

 

 

 

Java pattern 10

 

Following Java Program ask to the user to enter the number of rows to print the pyramid of stars(*):

 

import java.io.*;

import java.util.Scanner;

public class JavaPyramidPattern10

{

public static void main(String args[])

{

int i, space, rows, k=0;

Scanner scan = new Scanner(System.in);

System.out.print(“Enter Number of Rows : “);

rows = scan.nextInt();

for(i=1; i<=rows; i++)

{

for(space=1; space<=(rows-i); space++)

{

System.out.print(” “);

}

while(k != (2*i-1))

{

System.out.print(“* “);

k++;

}

k = 0;

System.out.println();

}

}

}

When the above Java Program is compile and executed, it will produce the following

output:

p10

 

 

 

 

 

 

 

 

 

 

 

 

 

Java Pattern 11

 

Write a program in Java to show christmas tree using package.

 

//First create a package

package pack3;
public class tree
{
public void disp()
{
int i,j,k,l;
for(i=1;i<=5;i++)
{
for(j=5;j>=i;j–)
{
System.out.print(” “);
}
for(k=1;k<=i;k++)
{
System.out.print(“*”);
}
for(l=i-1;l>=1;l–)
{
System.out.print(“*”);
}
System.out.print(“\n”);
}}

public void root()
{
int k,i,j;
for( i=1;i<=5;i++)
{
for( j=1;j<5;j++)
{
System.out.print(” “);
}
for(k=1;k<=3;k++)
{
System.out.print(“*”);
}
System.out.println(” “);
}}}

//Save this package with tree.java in bin

//Compile the program with this command like below:

E:\jdk1.6.0\bin>javac -d e:\jdk1.6.0\bin tree.java

 
//Now import the package e.g

import pack3.tree;
class christtree
{
public static void main(String args[])
{
tree a=new tree();
a.disp();
a.disp();
a.disp();
a.disp();
a.root();
}
}

 output:

p11

Related Posts

Comments are closed.