Java Program to Display the pattern like a diamond.

In this post, we will create two java program to display the pattern like a diamond. The first program will work on the Looping concept and the second will work on the recursion concept.

To understand these program you should have knowledge of following concepts of Core Java Programming.

  • For loop
  • While loop
  • If-else

How the Display the pattern like a diamond will work?

  • Define the value of rows.
  • Now run the first loop till row value.
  • And the second loop till the first loop value.
  • Print the half diamond.
  • The second half diamond using second and third step in reverse way.

Example:

  1. Input:
  2. Input number of rows : 5
  3. Output:
    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *

Example 1: Program to Display the pattern like a diamond using Loop

Enter the number of rows as an input. Now we use for Loops to print two equilateral triangles facing away from each other but with same base.

Here is the code of the java Program to Display the pattern like diamond. The java program is successfully compiled and run on windows system. The program output is also shown below.

import java.util.Scanner;
public class pattern_like_diamond {
    public static void main(String[] args) {
        int space = 1;
        Scanner sc = new Scanner(System.in);
        System.out.print("Input number of rows : ");
        int num = sc.nextInt();
        space = num-1;

        for (int j=1;j<=num;j++){

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

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

Output:

Input number of rows : 5
    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *

Example 2: Program to Display the pattern like a diamond using recursion

In this program, we will display the same pattern using recursion also. The pattern is divided into two portions. The increasing portion and the decreasing portion of the pattern. The below program displays the diamond pattern of stars using recursion.

import java.util.Scanner;
public class demo {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Input number of rows : ");
        int rows = sc.nextInt();
        
        diamond(rows);
    }
    private static int stars = -1;
    private static void diamond(int rows) {
        int space;
        if (rows<=0){
            return;
        }
        space = rows - 1;
        stars += 2;

        for(int c = 0; c < space; c++) {
            System.out.print(" ");
        }

        for(int c = 0; c < stars; c++) {
            System.out.print("*");
        }

        System.out.println();

        diamond(--rows);

        space = rows + 1;
        stars -= 2;

        for(int c = 0; c < space; c++) {
            System.out.print(" ");
        }

        for(int c = 0; c < stars; c++) {
            System.out.print("*");
        }

        System.out.println();
    }
}

Output:

Input number of rows : 7
      *
     ***
    *****
   *******
  *********
 ***********
*************
 ***********
  *********
   *******
    *****
     ***
      *

Recommended:

  1. Java Program to Check a year is a leap year or not
  2. Java Program to display the sum of first N natural numbers
  3. Java Program to find sum and average of N numbers
  4. Java Program to find a cube of an N given numbers.
  5. Java Program to Print sum of first N odd natural numbers
  6. Java Program to Display Right angle triangle using number

Leave a Comment

Your email address will not be published. Required fields are marked *

x