Software & Finance





Java - Pyramid of Stars





Here is Java Source code for displaying a pyramid of stars.

 

 

Source Code


import java.io.*;

import java.lang.*;

 

 

class Pyramid {  

 

    public static void main(String[] args) {

 

            String inpstring = "";

            InputStreamReader input = new InputStreamReader(System.in);

            BufferedReader reader = new BufferedReader(input);

 

            try

            {

                  System.out.print("Enter number of rows for pyramid:");

                  inpstring = reader.readLine();

                  int n = Integer.parseInt(inpstring, 10);

 

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

                  {

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

                              System.out.print(" ");

 

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

                              System.out.print("*");

 

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

                              System.out.print("*");

 

                        System.out.println();

                  }

 

                  System.out.println();

                  System.out.println();

            }

            catch (Exception e)

            {

                  e.printStackTrace();

            }

    }

}

 

 

Output


D:\Program Files\Java\jdk1.6.0_23\bin>Java Pyramid

Enter number of rows for pyramid:15

              *

             ***

            *****

           *******

          *********

         ***********

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

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

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

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

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

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

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

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

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