W pattern with Asterick and Space Example Java Program


W Pattern Example Program

import java.util.Scanner;

class StarPatternExampleOne {

    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 = (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 :
8
*              *
**            **
***          ***
****        ****
*****      *****
******    ******
*******  *******
****************