Java Write a Program to Create a Pyramid Structure

Let's First understand For loop:

For Loop:

It is structured around a finite set of repetitions of code. So if you have a particular block of code that you want to have run over and over again a specific number of times the For Loop is helpful.

Syntax:

          for(initilization; conditional expression; increment expression) {     //repetition code here }                  

eg:

          int x = 7; for (int i = 1; i <= x; i++){     // statements }                  

In this "int i=1" is an initialization part, the loop will iterate till condition is true that is (I <=x) replace the value of I and x(1<=7).

After this statement inside the loop body will be executed and in the end "i++" that is the value of "I" will be incremented (I will become 2).

This process will repeat every time till the condition (I <= x) is true.


Now lets understand how pattern are form:

          ROW (1) * --> COLUMN |   (2) ** |   (3) *** |   (4) ***** v   (5) *******                  

As we can see there are two things that need to be considered first the number of rows and second the number of columns:

We use two loops to construct such structures, first outer loop for number of rows and second inner loop for number of columns.

CODE LOGIC: First divide the pyramid into 3 triangle.

enter image description here

          ####$ ###$$@ ##$$$@@ #$$$$@@@ $$$$$@@@@                  

1> First triangle (White spaces represented by #)

          #### ### ## #                  

2> Second triangle (represented by $)

          $ $$  $$$ $$$$ $$$$$                  

3> Third triangle (represented by @)

            # @ @@ @@@ @@@@                      

Together it will make a pyramid structure


Lets begin :

Now lets breakdown our pattern problem:

enter image description here

          ######1 #####212 ####32123 ###4321234 ##543212345 #65432123456 7654321234567                  

We will break this into 3 triangles and every triangle will have its own loop inside the main loop for the number of rows. As we can see a number of rows required for our pattern are 7 so we will make the first loop to repeat till 7 rows are achieved.

main loop (for rows):

          int x = 7; for (int i = 1; i <= x; i++) { // main loop for number of rows      // other 3 loops will come insidethe mail loop  }                  

First inner loop: As we have seen in pattern logic that the first triangle will consist of white spaces or blank.

                      output eg: (representing white spaces with #)                                ######                                       #####                                ####                                 ###                             ##                             #          for (int j = 1; j <= x - i; j++)    // j represent a column number     System.out.print("   ");   // This will create blank spaces till the condition is true.     **condition explained:** j <= x - i                  

Value of "i" is "1" as this is the first time the main loop is running and ever since the value of "i" has not changed.

                      **replace values:**                  

when j = 1 : 1 <= 7-1 (will print) ######

//first iteration of main loop and inner loop.

after this control will go to the next loop i.e second inner loop:

j = 2 : 2 <= 7-1 (will print) #####

// on second iteration of main loop and inner loop.

after this control will go to the next loop i.e second inner loop:

j = 3 : 3 <= 7-1 (will print) ####

// on third iteration of main loop and inner loop.

after this control will go to the next loop i.e second inner loop:

j = 4 : 4 <= 7-1 (will print) ###

//on fourth iteration of main loop and inner loop.

after this control will go to the next loop i.e second inner loop:

j = 5 : 5 <= 7-1 (will print) ##
// on fifth iteration of main loop and inner loop.

after this control will go to the next loop i.e second inner loop:

j = 6 : 6 <= 7-1 (will print) #

//on sixth iteration of main loop and inner loop.

after this control will go to the next loop i.e second inner loop:

j = 7 : 7 <= 7-1 //end of first inner loop as (7<=6 is not true)


Second inner loop :

          for (int k = i; k >= 1; k--) //second triangle                 System.out.print((k >= 10) ?+ k : "  " + k);  // ternary operator is used to check the printing logic                  

NOTE:

          ternary operator syntax: result = testCondition ? value1 : value2                  

// if testcondition is true then result will get value1 else value 2

          ***DIY: (Take a paper pen)***     Implement for loop logic and replace variables with values:                  

First iteration: K=i (we know i=1) so k=1; k>=1 (Replacing variables we get 1>=1 which is true); execute statements.

Statement: System.out.print((k >= 10) ?+ k : " " + k); or if(1>=10)? then print value of "k" (that is 1)

else print vlaue of "k" with some spaces ahead k (in order to make the pyramid look even spaced)

NOTE: if you are getting confused with ternary operator then don't use it, you can simply wirte.

          System.out.print("  " + k); // will give same output                  

after this control will go to the next loop i.e third inner loop:

                      *Second innerloop is used to print this portion:*                                 1                             21                            321                           4321                          54321                               654321                        7654321                  

Third inner loop: This loop is used to print the third triangle.

                      2                             23                             234                             2345                             23456                             234567    for (int k = 2; k <=i; k++) //  loop column                   System.out.print((k >= 10) ?+ k : "  " + k);                              or                 System.out.print("  " + k)                  

lets see full code:

                      int x = 7;         for (int i = 1; i <= x; i++) {              // loop row          for (int j = 1; j <= x - i; j++)        // loop column (Triangle one)             System.out.print("   ");          for (int k = i; k >= 1; k--)            // loop column (Triangle two)             System.out.print(  "  " + k);          for (int k = 2; k <=i; k++)             // loop column (Triangle three)             System.out.print( "  " + k);          System.out.println();                   // used to go on new line (new row)     }                  

Java Write a Program to Create a Pyramid Structure

Source: https://stackoverflow.com/questions/7905617/explain-creating-a-pyramid-in-java

Belum ada Komentar untuk "Java Write a Program to Create a Pyramid Structure"

Posting Komentar

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel