fbpx

Courses Offered: Java, Python, Selenium, AWS, Salesforce, DevOps, Angular

Diamond Pattern – Magic of 3 Loop

Table of Contents

Introduction

Pattern is the best way to get familiar with programming. It is helpful for loop syntax and its use. Builds logic for coding. As a beginner, mostly students have fear factor towards it. It mainly depends on matrix fundamental.

Patterns are repetitive arrangements of data in matrix format. Different structure can be made out it. Star pattern, hollow pattern, matrix pattern, pyramid pattern and diamond pattern. Various alphabets and numerals. To represent beautiful repeated execution of data is possible with the help of loops.

Beginners find diamond pattern is critical one as it is collection of different patterns itself. In below content we will try to understand some techniques to generate this pattern.

Use of loops to draw pattern

As pattern is repeated representation. In java, loop statements helps to it. Loop iterates same block. Pattern displays in matrix form.  Matrix made up of rows and columns. While creating a pattern, minimum two nested loops are required. Nested outer for loop can attend the rows to proceed and similarly inner for loop to columns in every row. Diamond pattern is defined with 2 pyramid pattern.

Pyramid Pattern:

                   This pattern consist of three basic parts.

  1. We have to understand to control on row.
  2. When and how much space should be added before actual printing.
  3. How manage the number of columns get printed, as per the pattern demands.

As per the above pyramid concern,

  1. We required 5 rows to create this pyramid, it can vary as per user interest. For example, we are considering pyramid for 5 rows.
  2. Pyramid pattern represents equilateral triangle structure. To generate it on console, first find the required space for every row.

Observe, we will find for every coming row, number of spaces are decreasing. Achieve this space, we should check on possibility. Total row are five, if we minimize current row count from it, we will get number of spaces.

Row =1                size-row = space => 5-1 = 4

Row = 2               size-row = space => 5-2 = 3

Row =3                size-row = space => 5-3 = 2

Row = 4               size-row = space => 5-4 = 1

Row = 5               size-row = space => 5-5 = 0

By this we can reach to start space before data printing.

  1. Now last but important part is data. Data may be asterisk (*), may be numerals, or may be characters (alphabet).

Columns Manipulation

While performing pattern, skeleton is crucial. Once structure gets ready, we can add any data as per choice.

In pyramid, columns need to arrange with data to make it look like it. Hence, after adding sufficient spaces, next task to print columns. Above analysis says, very row prints increasing odd column count. To display those column, few calculation can give the result for column count. As row increase column count is also increasing by (2 * row) -1.

Row =1             space = 4             (2*row)-1 = (2*1) -1 = 1

Row =2             space = 3             (2*row)-1 = (2*2) -1 = 3

Row =3             space = 2             (2*row)-1 = (2*3) -1 = 5

Row =4             space = 1             (2*row)-1 = (2*4) -1 = 7

Row =5             space = 0             (2*row)-1 = (2*5) -1 = 9

Here, we successfully printed the pyramid pattern. This is the half done for diamond pattern.

Diamond Pattern:

This diamond pattern can be constructed with two, upward and downward pyramid. Upward pyramid, we have seen above to develop, now it’s turn to make a code for diamond. Diamond pattern’s upward part we find as pyramid. Before going to look for downward part, need to focus on few things.

While developing code, there are few ways.

  1. In first, we can take diamond with 4 patterns altogether. But here number of for loops are increasing, which maximize the execution time.
  2. In another way, within three for loops we can draw diamond. By code, it will seems like 5 for loops. But actually executing loops are 3 at a time. Let’s discuss, how it is happening?

    While creating structure, we will consider whole matrix to draw diamond. Whichever size will be given for diamond, that many row will be accommodated by it. Henceforth, upward pyramid will be drawn in upper half and down ward pyramid will be in lower half. By doing this, one row will be skipped for better representation. Otherwise one extra middle row will be shown, which may minimizes the charm. Here, till the middle row, upward pyramid will be applied.

    Down ward pyramid,  To achieve this, we need to apply more logic.

    In down ward pyramid, we take the basic thing in consideration that we are done with half of the code. We are on row 6 for execution. From here opposite logic begins. Again need to analyze, what is requirement for row, space and column execution. Everything will revert. Somehow 1st row and last row are absolute similar. Space is also same in reverse order but on console we cannot move on either way. We can write only from left to right and row from top to bottom. We are unable to print in reverse order like right to left or bottom to top. Let’s calculate and decide. For analysis again go with 10 row matrix for diamond, where only 9 rows and 9 columns will display.

    R = 6           sp = (6-5) = 1                   cols = 2*(10 – 6 ) -1 = 8-1 = 7

    R = 7           sp = (7-5) = 2                   cols = 2*(10 – 7) -1 = 6-1 = 5

    R = 8           sp = (8-5) = 3                    cols = 2*(10 – 8) – 1 = 3

    R = 9           sp = (9-5) = 4                   cols = 2*(10 – 9) – 1 = 1

    All columns are in odd numbers. Formula for space, increasing row and decreasing columns. Row – mid returns number of space. For columns, 2*(size – row)-1 provides column number. This results in downward pyramid. With this all efforts the beautiful diamond is generated.

    Conclusion

    With different manipulation of statements, we can achieve several amazing patterns. With the help of above skeleton and adding arrangements of number and alphabets, we can draw various diamond patterns. Loop magician completes our magic with numerous formation of patterns.

     

    Java by Kiran Academy offers flexible IT courses in Pune, including Java, Python course in Pune, .NET, Software Testing, MEAN, MERN and more. Offline and Online classes are available.

    Our Campus Drive

    Subscribe to Us on YouTube

    Get daily updates on:
    • Technical
    • Interview 
    • Upcoming Campus 
    • Job-Related Fairs

    Are you looking for a better job?
    Talk to our Career Expert Now !


    Recommended Videos

    Request a Callback

    For any Queries? Talk to our Experts
    +918888809416

    Available 24x7 to answer your queries

    Register Now