Introduction
A diamond pattern is a common exercise that helps programmers get comfortable with using loops and controlling output format. The diamond pattern consists of two parts: an upper triangle and an inverted lower triangle, which together form the shape of a diamond.
Problem Statement
Create a Java program that:
- Accepts the size of the diamond (which determines the height).
- Prints a diamond-shaped pattern using stars (
*
).
Example:
- Input:
size = 5
- Output:
* *** ***** ******* ********* ******* ***** *** *
Solution Steps
- Input the Size of the Diamond: The size will control the height and width of the diamond.
- Use Nested Loops: The first set of loops will handle the upper triangle (including the middle row), and the second set will handle the lower inverted triangle.
- Display the Diamond: Print stars and spaces in such a way that the output forms a diamond pattern.
Java Program
// Java Program to Print a Diamond Pattern
// Author: [Your Name]
import java.util.Scanner;
public class DiamondPattern {
public static void main(String[] args) {
// Step 1: Accept the size of the diamond
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of the diamond: ");
int size = sc.nextInt();
// Step 2: Print the upper part of the diamond (including the middle row)
for (int i = 1; i <= size; i++) {
// Step 3: Print spaces for alignment
for (int j = i; j < size; j++) {
System.out.print(" ");
}
// Step 4: Print stars
for (int j = 1; j <= (2 * i - 1); j++) {
System.out.print("*");
}
// Move to the next line after printing each row
System.out.println();
}
// Step 5: Print the lower part of the diamond (inverted triangle)
for (int i = size - 1; i >= 1; i--) {
// Step 6: Print spaces for alignment
for (int j = size; j > i; j--) {
System.out.print(" ");
}
// Step 7: Print stars
for (int j = 1; j <= (2 * i - 1); j++) {
System.out.print("*");
}
// Move to the next line after printing each row
System.out.println();
}
// Closing the scanner object
sc.close();
}
}
Explanation
Step 1: Input Size
- The program begins by accepting the size of the diamond from the user, which determines the height of the diamond.
Step 2-4: Print the Upper Part
- The first outer loop handles printing the upper part of the diamond, including the middle row.
- A nested loop prints spaces for alignment, ensuring that the stars form a centered triangle.
- Another loop prints stars, starting from 1 star on the first row and increasing by 2 stars per row.
Step 5-7: Print the Lower Part
- The second outer loop prints the lower part of the diamond (inverted triangle).
- The inner loop again prints spaces to keep the inverted triangle aligned.
- Another loop prints stars, starting with
2*size - 3
stars and decreasing by 2 stars per row.
Output Example
For size = 5
, the output is:
*
***
*****
*******
*********
*******
*****
***
*
For size = 6
, the output is:
*
***
*****
*******
*********
***********
*********
*******
*****
***
*
Conclusion
This Java program prints a diamond-shaped star pattern by using nested loops to print spaces and stars. It first prints the upper triangle and then the lower inverted triangle to complete the diamond. This exercise is helpful for learning how to manipulate output using loops and patterns in Java.
Comments
Post a Comment
Leave Comment