Diamond pattern in java

diamond pattern in java is one of the interesting pattern program for beginner and intermediate learners of programming.

If you see diamond pattern you will see it is combination of two triangle pattern that is upper and lower triangle.

Here we will see different diamond pattern programs in java.

We will start with basic pattern in java

1. Triangle Pattern

Let first see to draw upper pattern.

  • Get No of rows to print
  • loop to number or rows using outer loop for (int row = 1; row <= rows; row++)
  • print odd numbers of * in each column for (int col = 1; col <= 2 * row - 1; col++)
  • Go to next Line and repeat step 1 to 3

Output

This will Print above pattern.

But we require pattern should be at center.

2. Triangle Pattern at center

Lets modify above program and add appropriate space so that we can print center triangle.

  • Get No of rows to print
  • loop to number or rows using outer loop for (int row = 1; row <= rows; row++)
  • Print number of spaces using this loop for (int col = 1; col <= rows-row; col++)
  • print odd numbers of * in each column for (int col = 1; col <= 2 * row - 1; col++)
  • Go to next line and repeat step 1-3

Output

This will print correct start pattern.

3. Lower Triangle Pattern

Lets see another program to print lower triangle program.

This program is similar to upper triangle only we have to reverse the outer loop

Output

This is printing a good lower start triangle.

4. Diamond Pattern

Lets combine both upper and lower star triangle to make diamond.

Our AIM is to print diamond pattern of stars.

Lets see java program to print diamond pattern of stars .

Here Upper triangle is as it is only in lower triangle we started outer loop from one less position that is row=rows-1

for (int row = rows - 1; row > 0; row--)

Output

After combining upper and lower triangle we got our diamond pattern.

5. Diamond Pattern with variation

As we know the concept of creating upper and lower triangle pattern.

we can replace * with any numbers les dive in with combining other symbols.

Output

Let combine other symbols.

Output

6. Diamond Pattern altering column

6.1 Altering Lower Triangle columns

Here upper triangle printing one time less then upper triangle.

Output

6.2 Altering Complete diamond pattern

Here two symbols are changed alternately in lower triangle.

Output

7. Diamond Pattern with row alteration

7.1 Altering lower triangle

Output

7.2 Altering all triangle row

Output

8. Diamond Pattern with Numbers

8.1 Number column wise

Output

8.2 Number row wise

Output

9. Diamond Pattern for String

  • Create a integer variable with value 64 ( we started loop with 1 so taken value 64)
  • Inside loop print System.out.print((char) (character + col));

Output

  • Create a integer variable with value 64 ( we started loop with 1 so taken value 64)
  • Inside loop print System.out.print((char) (character + row));

Output