Diamond pattern with Asterick and Space Example Java Program


Diamond Pattern Example Program

import java.util.Scanner;

class StarPatternExampleThree {

    public static void main(String[] args) {
        System.out.println("Enter the number of rows for pattern : ");
        Scanner scanner = new Scanner(System.in);
        int rowsCount = scanner.nextInt();

        int spacesCount = 0;
        for (int i = rowsCount; i > 0; i--) {
            for (int j = i; j > 0; j--) {
                System.out.print("*");
            }
            for (int j = 0; j < spacesCount; j++) {
                System.out.print(" ");
            }
            for (int j = i; j > 0; j--) {
                System.out.print("*");
            }
            spacesCount = spacesCount + 2;
            System.out.println("");
        }

        spacesCount = (rowsCount * 2) - 2;
        for (int i = 0; i < rowsCount; i++) {
            for (int j = 0; j <= i; j++) {
                System.out.print("*");
            }
            for (int j = 0; j < spacesCount; j++) {
                System.out.print(" ");
            }
            for (int j = 0; j <= i; j++) {
                System.out.print("*");
            }
            spacesCount = spacesCount - 2;
            System.out.println("");
        }
    }
}

Sample Output

Enter the number of rows for pattern :
10
********************
*********  *********
********    ********
*******      *******
******        ******
*****          *****
****            ****
***              ***
**                **
*                  *
*                  *
**                **
***              ***
****            ****
*****          *****
******        ******
*******      *******
********    ********
*********  *********
********************