Tutorials Java Drawing a pyramid using loops in Java

Drawing a pyramid using loops in Java

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

          * 
         * * 
        * * * 
       * * * * 
      * * * * * 
     * * * * * * 
    * * * * * * * 
   * * * * * * * * 
  * * * * * * * * * 
 * * * * * * * * * * 
Total
0
Shares
guest
5 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Bikram kumar swain
Bikram kumar swain
7 years ago

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

Kim Kelly Rapiz
Kim Kelly Rapiz
7 years ago

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

Navjot Singh
Navjot Singh
6 years ago
Feeling Programmer
Feeling Programmer
7 years ago

How about this print ? Help me please :DD

javalschool
javalschool
6 years ago

Nice Article sir Thanks for sharing the article

Previous Post
Java Tutorials

Java Constructor Tutorial and Examples

Next Post
Java Tutorials

Calculating factorial of number in Java

Related Posts
By clicking “Allow All”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. Cookie Notice
Allow All
5
0
Would love your thoughts, please comment.x
()
x