Write a Java Program to Print Diamond Pattern.
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.
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.
/*
* Diamond Pattern Program in Java
*/
import java.util.Scanner;
public class Diamond
{
public static void main(String args[])
{
int n, i, j, space = 1;
System.out.print("Enter the number of rows: ");
Scanner s = new Scanner(System.in);
n = s.nextInt();
space = n - 1;
for (j = 1; j <= n; j++)
{
for (i = 1; i <= space; i++)
{
System.out.print(" ");
}
space--;
for (i = 1; i <= 2 * j - 1; i++)
{
System.out.print("*");
}
System.out.println("");
}
space = 1;
for (j = 1; j <= n - 1; j++)
{
for (i = 1; i <= space; i++)
{
System.out.print(" ");
}
space++;
for (i = 1; i <= 2 * (n - j) - 1; i++)
{
System.out.print("*");
}
System.out.println("");
}
}
}
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.
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.
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.
$ 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”.
- Practice Information Technology MCQs
- Practice Programming MCQs
- Check Java Books
- Practice BCA MCQs
- Check Programming Books