Java Programming | Drawing a pyramid using loops

Introduction

This is a tutorial for drawing a pyramid using loop in java. The program is given below that prints the pyramid directly. The program is extendable. Go enjoy the program. Lets begin…….

Program for drawing pyramid using loops in java.

// the name of our class its public
public class Pyramid {
	//void main
		public static void main (String[] args)
		{
			//declare int
			int i,j,s=5,m;

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

Output

NOTE:- The output shown below is of pyramid, my blog is deleting the spaces automatically so its not drawn properly here, but i promise that the program above will draw it in your software.

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

How does it work

No need to input anything. Directly prints the pyramid.

Extending it

The program can be extended by using different symbols instead of ‘*’ in program. Go ahead and extend it…..

Even you can invert the pyramid and make parallelogram using two pyramids….

Explanation.

  1. Declare the class as public
  2. Add the void main function.
  3. Add loops and print the pyramid.

At the end.

You learnt creating the Java program for Drawing a pyramid using loops. So now enjoy the program.

Please comment on the post and share it.
And like it if you liked.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s