Signup/Sign In

Java Program to Print the Diamond Pattern

In this tutorial, we will see how to print the diamond pattern in Java. First we will ask the user to initialize the number of rows. Then, we will use loops to print two equilateral triangles facing away from each other but with the same base. 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: 4

Enter the symbol: *

Output: The pattern is:

*

* *

* * *

* * * *

* * *

* *

*

This can be done by using the following methods:

Approach 1: Using For Loop

Approach 2: Using While loop

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

Program 1: Java Program to Print the Diamond Pattern

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

Algorithm:

  1. Start
  2. Create an instance of the Scanner class.
  3. Declare variables to store the number of rows and the pattern symbol.
  4. Ask the user to initialize these variables.
  5. Use two for loops to print the pattern.
  6. Inside each for loop, use two more for loops to print the pattern.
  7. Use one for loop to print the space and the other for loop to print the pattern.
  8. Use the first for loop to print the upper diamond pattern.
  9. Use the second for loop to print the lower diamond pattern.
  10. Display the result.
  11. Stop

The below example illustrates the implementation of the above algorithm.

//Java Program to print the diamond pattern
import java.util.Scanner;
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(); 
        System.out.println("Enter the Symbol : ");
        char c = sc.next().charAt(0);
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n-i;j++)
            {
                System.out.print(" ");
            }
            for(int j=1;j<=i*2-1;j++)
            {
                System.out.print(c);
            }
            System.out.println();
        } 
        for(int i=n-1;i>0;i--)
        {
            for(int j=1;j<=n-i;j++)
            {
                System.out.print(" ");
            }
            for(int j=1;j<=i*2-1;j++)
            {
                System.out.print(c);
            }
            System.out.println();
        }
   }
}


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

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

Program 2: Java Program to Print the Diamond Pattern

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

Algorithm:

  1. Start
  2. Create an instance of the Scanner class.
  3. Declare variables to store the number of rows and the pattern symbol.
  4. Ask the user to initialize these variables.
  5. Use two while loops to print the pattern.
  6. Use the first while loop to print the upper diamond pattern.
  7. Use the second while loop to print the lower diamond pattern.
  8. Display the result.
  9. Stop

The below example illustrates the implementation of the above algorithm.

//Java Program to print the diamond pattern
import java.util.Scanner;
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(); 
       System.out.println("Enter the Symbol : ");
       System.out.println("");
       char c = sc.next().charAt(0);
       int i=1;
       int j;
       while(i<=n)
       {
           j=1;
           while(j++<=n-i)
           {
               System.out.print(" ");
           }
           j=1;
           while(j++<=i*2-1)
           {
               System.out.print(c);
           }
           System.out.println();
           i++;
        } 
        i=n-1;
        while(i>0)
        {
           j=1;
           while(j++<=n-i)
           {
               System.out.print(" ");
           } 
           j=1;
           while(j++<=i*2-1)
           {
              System.out.print(c);
            }
             System.out.println();
             i--;
         }
    }
}


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

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



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.