Java Program to Print a Diamond Shape with Numbers

1. Introduction

This blog post will guide you through creating a Java program that prints a diamond shape filled with numbers, based on user input for the size. This pattern combines concepts of loops and conditionals to manipulate numbers and spaces, producing a visually appealing diamond shape. It's an excellent exercise for enhancing understanding of Java programming by generating patterns using numbers.

2. Program Steps

1. Import the Scanner class to read the height of the diamond from the user.

2. Prompt the user to enter the height of the diamond, which will determine its overall size.

3. Use nested loops to print the upper half of the diamond, incrementing numbers in each row.

4. Repeat a similar process for the lower half of the diamond, decrementing numbers to complete the pattern.

5. Compile and run the program, then input the desired height to see the diamond shape filled with numbers.

3. Code Program

import java.util.Scanner; // Import Scanner class

public class DiamondWithNumbers {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in); // Create Scanner object
        System.out.print("Enter the height of the diamond: "); // Prompt for user input
        int height = scanner.nextInt(); // Read the height from user
        int n = height / 2 + 1; // Calculate the number of rows for the upper half

        // Print the upper half of the diamond
        for (int i = 1; i <= n; i++) {
            // Print leading spaces
            for (int j = 1; j <= n - i; j++) {
                System.out.print(" ");
            }
            // Print numbers
            for (int j = 1; j <= 2 * i - 1; j++) {
                System.out.print(j);
            }
            System.out.println();
        }

        // Print the lower half of the diamond
        for (int i = n - 1; i >= 1; i--) {
            // Print leading spaces
            for (int j = 1; j <= n - i; j++) {
                System.out.print(" ");
            }
            // Print numbers
            for (int j = 1; j <= 2 * i - 1; j++) {
                System.out.print(j);
            }
            System.out.println();
        }
        scanner.close(); // Close the scanner
    }
}

Output:

(For height = 7)
   1
  123
 12345
1234567
 12345
  123
   1

Explanation:

1. The program starts by importing the Scanner class and prompting the user for the height of the diamond. This height determines how large the diamond will be, with the actual diamond being constructed of numbers instead of asterisks.

2. It calculates the number of rows for the upper half of the diamond (n = height / 2 + 1). This calculation ensures that both halves of the diamond are symmetrical.

3. For the upper half, an outer loop iterates over each row. Nested within this loop, one loop prints the leading spaces to center the numbers, and another loop prints incrementing numbers starting from 1 up to (2 * i - 1) for each row, forming the upper half of the diamond.

4. The process is then inverted for the lower half of the diamond, with the outer loop iterating in reverse to decrement the number of numbers printed in each row and another loop adding the leading spaces as needed.

5. After printing the numbers for each row, a newline character is used to move to the next row, continuing this pattern until the entire diamond shape is printed.

6. Finally, the Scanner object is closed to prevent resource leaks, adhering to best practices in handling user input in Java programs.

Comments