Java Program to Print Diamond Pattern

Problem Description

Write a Java Program to Print Diamond Pattern.

Problem Solution

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

Program/Source Code

Here is the source code of the Java Program to Print Diamond Pattern. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. /*
  2.  * Diamond Pattern Program in Java
  3.  */
  4.  
  5. import java.util.Scanner;
  6. public class Diamond
  7. {
  8.     public static void main(String args[]) 
  9.     {
  10.         int n, i, j, space = 1;
  11.         System.out.print("Enter the number of rows: ");
  12.         Scanner s = new Scanner(System.in);
  13.         n = s.nextInt();
  14.         space = n - 1;
  15.         for (j = 1; j <= n; j++) 
  16.         {
  17.             for (i = 1; i <= space; i++) 
  18.             {
  19.                 System.out.print(" ");
  20.             }
  21.             space--;
  22.             for (i = 1; i <= 2 * j - 1; i++) 
  23.             {
  24.                 System.out.print("*");                
  25.             }
  26.             System.out.println("");
  27.         }
  28.         space = 1;
  29.         for (j = 1; j <= n - 1; j++) 
  30.         {
  31.             for (i = 1; i <= space; i++) 
  32.             {
  33.                 System.out.print(" ");
  34.             }
  35.             space++;
  36.             for (i = 1; i <= 2 * (n - j) - 1; i++) 
  37.             {
  38.                 System.out.print("*");
  39.             }
  40.             System.out.println("");
  41.         }
  42.     }
  43. }
Program Explanation

1. This program asks the user to enter the number of rows for a diamond pattern. It then reads the input and stores it in the variable ‘n‘.
2. The program initializes integer variables ‘i‘, ‘j‘ and ‘space‘, where ‘space‘ is initialized to 1.
3. The program enters a for loop that iterates from ‘j’ = 1 to ‘n’. Within this loop, there are two nested loops.
4. The first nested loop prints spaces before the asterisks in each row. The number of spaces to be printed is stored in the ‘space‘ variable, which is decremented after each iteration.
5. The second nested loop prints asterisks for each row, and the number of asterisks is calculated by the expression ‘2 * j – 1’.
6. After each row is printed, the program moves to a new line using the System.out.println() statement.
7. After the first loop is complete, the program initializes the ‘space‘ variable to 1 again, and enters a second for loop, which is similar to the first loop but prints the inverted pyramid.
8. Finally the program terminates after both loops are completed.

Time Complexity: O(n2)
The time complexity of this program is O(n2) because it has two nested loops that run n times each, which results in n2 total iterations.

advertisement
advertisement

Space Complexity: O(1)
The space complexity of this program is O(1) because it uses a fixed amount of memory regardless of the size of the input. The only variable declared is n, and the other variables (i, j, and space) are all integers, which require a fixed amount of memory.

Run Time Testcases

Testcase 1: In this case, we enter the number of rows as “8” to print the diamond pattern.

$ javac Diamond.java
$ java Diamond
 
Enter the number of rows: 5
 
       *
      ***
     *****
    *******
   *********
  ***********
 *************
***************
 *************
  ***********
   *********
    *******
     *****
      ***
       *

Testcase 2: In this case, we enter the number of rows as “5” to print the diamond pattern.

Note: Join free Sanfoundry classes at Telegram or Youtube
$ javac Diamond.java
$ java Diamond
 
Enter the number of rows: 5
    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *

To practice programs on every topic in Java, please visit “Programming Examples in Java”, “Data Structures in Java” and “Algorithms in Java”.

advertisement

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.