Java Program to Print Diamond of Stars and Numbers

1. Introduction

The diamond pattern is a classic pattern in programming that helps in understanding nested loops and conditionals. This Java program will generate a diamond pattern of stars and a diamond pattern of numbers.

2. Program Steps

1. Define the number of rows for the diamond patterns.

2. Use nested loops to print spaces and stars for the diamond of stars pattern.

3. Use nested loops to print spaces and numbers for the diamond of numbers pattern.

4. Ensure the middle of the diamond aligns with the number of rows for both patterns.

3. Code Program

public class DiamondPattern {

    public static void main(String[] args) {
        int n = 4; // The middle of the diamond

        // Diamond of Stars
        System.out.println("Diamond Of Stars");
        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; j++) {
                System.out.print("*");
            }
            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; j++) {
                System.out.print("*");
            }
            System.out.println();
        }

        // Diamond of Numbers
        System.out.println("Diamond Of Numbers");
        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; j++) {
                System.out.print(i);
            }
            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; j++) {
                System.out.print(i);
            }
            System.out.println();
        }
    }
}

Output:

Diamond Of Stars
   *
  ***
 *****
*******
 *****
  ***
   *
Diamond Of Numbers
   1
  222
 33333
4444444
 33333
  222
   1

Explanation:

1. The main method initializes an integer n that represents the total rows for the diamond patterns.

2. For the diamond of stars, the first for loop creates the upper half of the diamond by printing spaces and then stars. The spaces decrease and the stars increase as we move down each row.

3. The second for loop for the diamond of stars creates the lower half by reversing the process: decreasing stars and increasing spaces.

4. For the diamond of numbers, a similar approach is used, but instead of stars, the current row number is printed. The same logic applies to increasing and decreasing the count of numbers as with the stars.

5. Each inner loop uses System.out.print() to print spaces, stars, or numbers, and System.out.println() to move to the next line after each row.

6. This results in two diamond shapes, one made of stars and the other of incrementing numbers, printed to the console.


Comments