Java program to print a diamond pattern with stars

Java program to print a diamond pattern:

In this post, we will write a Java program to print a diamond pattern. The program will take the height of the pattern as input from the user and print the diamond pattern using star. You can also use any other characters to print this pattern.

To learn how to print this pattern, let’s learn how the algorithm works. If you understand the algorithm, you can write the program without any effort.

Algorithm to print a diamond pattern:

To write the algorithm, first, let’s check what the pattern looks like:

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

This is a diamond pattern of height 6. We are using blank spaces and stars to print this pattern. To find out how many blank spaces are printed, let me replace all blank spaces with #:

#####*
####***
###*****
##*******
#*********
***********
#*********
##*******
###*****
####***
#####*

We have to break the program into two parts. The first part will print the pattern from row 1 to 6 and the second part will print the pattern from row 7 to 11.

Upper half part:

#####*
####***
###*****
##*******
#*********
***********

To print this pattern,

  • Take the total number of rows as input from the user
  • We will use one loop from i = 1 to i = rows, where rows is the number of rows or the pattern.
    • Inside the loop, it will run two loops to print the blank spaces and stars.
    • The first loop will run from j = 1 to j = rows - i. This loop will print the white spaces.
    • The second loop will run from j = 1 to j = 2*i - 1. It will print the stars.
    • At the end of each iteration of the loop, it will print one newline character to move to the next line.

Lower half part:

Similar to the upper half part, we have to print the lower half part.

#*********
##*******
###*****
####***
#####*
  • This is bit different than the previous pattern. We will use one outer loop and two inner loops to print it.
  • The outer loop will run from i = 1 to i = rows - 1. On each iteration, it will point to each row of the pattern.
    • The first inner loop will run from j = 1 to j = i. It will print the blank spaces.
    • The second inner loop will run from j = 1 to j = 2*(rows - i) - 1. It will print the stars.
    • Similar to the above example, it will print a new line at the end of each iteration of the outer loop.

Example 1: Java program to print the diamond pattern with for loops:

The below Java program prints the diamond pattern by using for loops:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int i, j, height;
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter the height of the pattern: ");
        height = scanner.nextInt();

        for (i = 1; i <= height; i++) {
            for (j = 1; j <= height - i; j++) {
                System.out.print(" ");
            }
            for (j = 1; j <= 2 * i - 1; j++) {
                System.out.print("*");
            }
            System.out.println();
        }

        for (i = 1; i <= height - 1; i++) {
            for (j = 1; j <= i; j++) {
                System.out.print(" ");
            }
            for (j = 1; j <= 2 * (height - i) - 1; j++) {
                System.out.print("*");
            }
            System.out.println();
        }

    }
}
  • The integer i and j are used with the loops. The integer variable height is used to keep the height of the pattern. It uses a Scanner object to read the height of the pattern.
  • It uses two for loops to print the pattern. Each loop has two inner for loops. The first loop prints the first half of the diamond pattern and the second loop prints the second half of the pattern.

It will print output as below:

Enter the height of the pattern: 
5
    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *

Enter the height of the pattern: 
6
     *
    ***
   *****
  *******
 *********
***********
 *********
  *******
   *****
    ***
     *

Example 2: Java program to print the diamond pattern using while loops:

We can also use while loops to write the above program. We can easily change the above program to use while loops. The while loops checks one condition and executes the code of the loop. We can use the same variables to change the for loops to while loops.

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int i, j, height;
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter the height of the pattern: ");
        height = scanner.nextInt();

        i = 1;
        while (i <= height) {
            j = 1;
            while (j <= height - i) {
                System.out.print(" ");
                j++;
            }

            j = 1;
            while (j <= 2 * i - 1) {
                System.out.print("*");
                j++;
            }

            System.out.println();
            i++;
        }

        i = 1;
        while (i <= height - 1) {
            j = 1;
            while (j <= i) {
                System.out.print(" ");
                j++;
            }

            j = 1;
            while (j <= 2 * (height - i) - 1) {
                System.out.print("*");
                j++;
            }

            System.out.println();
            i++;
        }

    }
}

It will give similar results.

Enter the height of the pattern: 
5
    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *

Enter the height of the pattern: 
6
     *
    ***
   *****
  *******
 *********
***********
 *********
  *******
   *****
    ***
     *

Java example to print diamond pattern

You might also like: