M pattern with Asterick and Space Example Java Program


M Pattern Example Program

import java.util.Scanner;

class StarPatternExample {

    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("");
        }
    }
}

Sample Output

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