Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Java Program to Print Half Diamond Star Pattern

In this tutorial, we will see how to print the half diamond star pattern in Java. First, we will ask the user to initialize the number of rows. Then, we will use loops to print the half diamond star pattern. But before moving further, if you are not familiar with the concept of the loops in java, then do check the article on Loops in Java.

Input: Enter the number of rows: 7

Output: The pattern is:

*

**

***

****

*****

******

*******

******

*****

****

***

**

*

This can be done by using the following methods:

Approach 1: Using a For Loop

Approach 2: Using a While loop

Approach 3: Using a do-while loop

Let us look at each of these approaches for a better understanding

Program 1: Print Half Diamond Star Pattern

In this program, we will see how to print the half diamond star pattern in java using a for loop.

Algorithm:

  1. Start

  2. Create an instance of the Scanner class.

  3. Declare a variable to store the number of rows.

  4. Ask the user to initialize the variable.

  5. Use a for loop to print the pattern.

  6. The outer loops iterate through rows and inner loops iterate through columns

  7. The first outer for loop will print the half of the half diamond pattern, the second outer for loop will print the remaining half of the half diamond pattern.

  8. The first outer for loop iterates until the condition i<=n is false, the inner loop will display the character until the condition j<i. The first outer loop will print this pattern.

  9. The second outer for loop iterates until the condition i>0 is false, the inner loop will display character until j<=i, the second outer for loop will print this pattern.

  10. Display the result.

  11. Stop

The below example illustrates the implementation of the above algorithm.

//Java Program to Print the Half Diamond Star Pattern
import java.util.*;
public class Main
{
     public static void main(String []args)
     {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the number of rows: ");
        int n=sc.nextInt(); 
        int i, j;
        for (i = 0; i < n; i++)
        {
             for (j = 0; j < i + 1; j++)
                System.out.print("*");
           System.out.print("\n");
        }
        for (i = 1; i < n; i++) 
        {
            for (j = i; j < n; j++)
               System.out.print("*");
          System.out.print("\n");
        }
     }
}


Enter the number of rows: 4
*
**
***
****
***
**
*

Program 2: Print Half Diamond Star Pattern

In this program, we will see how to print the half diamond star pattern in java using a while loop.

Algorithm:

  1. Start

  2. Create an instance of the Scanner class.

  3. Declare a variable to store the number of rows.

  4. Ask the user to initialize the variable.

  5. Use a while loop to print the pattern.

  6. The first while loop iterates until i<=n.

  7. Initialize the inner loop variable to 1. The inner while loop will iterate till j++<=n. If the condition satisfies it will print the pattern.

  8. The second while loop iterates until i>0.

  9. Initialize the inner loop variable to 1. The inner while loop will iterate till j++<=i. If the condition satisfies it will print the pattern.

  10. Display the result.

  11. Stop

The below example illustrates the implementation of the above algorithm.

//Java Program to Print the Half Diamond Star Pattern
import java.util.*;
public class Main
{
     public static void main(String []args)
     {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the number of rows: ");
        int n=sc.nextInt(); 
        int i=1,j;
        while(i<=n)
        {
           j=1;
           while(j++<=i)
           {
               System.out.print("*");
           }
            System.out.println();
            i++;
        } 
        i=n-1;
        while(i>0)
        {
           j=1;
           while(j++<=i)
           {
                System.out.print("*");
           }
           System.out.println();
           i--;
        } 
     }
}


Enter the number of rows: 7
*
**
***
****
*****
******
*******
******
*****
****
***
**
*

Program 3: Print Half Diamond Star Pattern

In this program, we will see how to print the half diamond star pattern in java using a do-while loop.

Algorithm:

  1. Start

  2. Create an instance of the Scanner class.

  3. Declare a variable to store the number of rows.

  4. Ask the user to initialize the variable.

  5. Use two do-while loops to print the pattern.

  6. The first do-while loop iterates untill ++i<=n.

  7. Initialize the inner loop variable to 1. The inner do-while loop will iterate till ++j<=i. If the condition satisfies it will print the pattern.

  8. The second do-while loop iterates until --i>0.

  9. Initialize the inner loop variable to 1. The inner do-while loop will iterate till ++j<=i. If the condition satisfies it will print the pattern.

  10. Display the result.

  11. Stop

The below example illustrates the implementation of the above algorithm.

//Java Program to Print the Half Diamond Star Pattern
import java.util.*;
public class Main
{
     public static void main(String []args)
     {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the number of rows: ");
        int n=sc.nextInt(); 
        int i=1;
        int j; 
        do 
        {
            j=1;
            do
            {
               System.out.print("*"); 
            }while(++j<=i); 
            System.out.println();
        } while(++i<=n); 
        i=n-1; 
        do 
        {
            j=1;
            do
            {
                System.out.print("*"); 
            }while(++j<=i); 
            System.out.println(); 
        } while(--i>0); 
     }
}


Enter the number of rows: 7
*
**
***
****
*****
******
*******
******
*****
****
***
**
*