Below example prints star like pyramid structure using loop in java.

 public class Pyramid {
	// void main
	public static void main(String[] args) {
		int depth = 10;
		int s = depth, m;

		for (int i = 1; i < = depth; i++) {
			m = s;
			while (s > 0) {
				System.out.print(" ");
				s--;
			}
			for (int j = 1; j < = i; j++) {
				System.out.print("* ");
			}
			System.out.print("\n");
			s = m - 1;
		}
	}
}

Output

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

-Advertisement-

Join The Discussion

Please note: We reserve the right to delete comments that contains snarky remarks, offensive or off-topic. To know more read our comments policy.
  • Nice Article sir Thanks for sharing the article

  • Feeling Programmer

    How about this print ? Help me please :DD

  • Kim Kelly Rapiz

    how is this?
    *****
    ****
    ***
    **
    *
    **
    ***
    ****
    *****

  • Bikram kumar swain

    i want code for
    * *
    ** **
    *** ***
    **** ****
    *********