Pyramid Pattern – Nested Loop – 2

pyramid pattern

 

This all programs is same as our last pyramid pattern, but all have some small different.

In this image, we found 12 different programs on pyramid pattern. But Logic of all programs is same.

Let’s understand one program and create that program in this blog.

pyramid pattern

This is our program image.

Before starting on this program, you must have to knowledge of  pattern and logical nested loop.

Let’s see why we need?

First Step: Create Left Alignment Pattern Program

54321
5432
543
54
5

 

for(int i=1;i<=5;i++)         // Step of above example

{

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

                {

                                System.out.print(j);

}

System.out.println();

}

 

 

 

 

Second Step: Create Right Alignment Pattern Program using K loop.

54321
5432
543
54
5

 

int sp=0;

for(int i=1;i<=5;i–)           // Step of above example

{

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

                {

                System.out.print(“ ”);    // 1 space

}

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

                {

                                System.out.print(j);

}

System.out.println();

sp++;

}

 

Third Step: Create Pyramid Pattern Program using single space.

Just Change System.out.print(j);  line into System.out.print(“ ”+j);  // 1 Space before or after j variable.

 

 

If you have problem on this blog, you can directly comment on it. This tutorial will help you understand the concepts of java loop programs.

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

229total visits,4visits today


mayank

Hello, I am Maddy a Java blogger, trainer and developer

Related articles

Leave a Reply

EmailEmail
PrintPrint