Pattern Hour Glass

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int nr = 2 * n + 1;
int nst = nr;
int nsp = 0;

	int row = 1;
	while (row <= nr) {
		int val = 5;

		for (int csp = 1; csp <= nsp; csp++) {
			System.out.print(" ");
		}

		for (int cst = 1; cst <= nst; cst++) {

			if (cst <= nst / 2) {
				val--;
			} else {
				val++;
			}
			System.out.print(val);

		}
		
		System.out.println();

		if (row <= nr / 2) {
			nsp=nsp+1;
			nst=nst-1;
		} else {
			nsp=nsp-1;
			nst=nst+1;
			
		}
		row++;
	}
}

Please help me in this . this the code for pattern hour glass

@amangaur078,

Pattern Hack : Always first Try to first print pattern by ignoring the value to be printed then accommodate your value in that pattern. For e.g.,

                      5 4 3 2 1 0 1 2 3 4 5
                        4 3 2 1 0 1 2 3 4 
                          3 2 1 0 1 2 3 
                            2 1 0 1 2 
                              1 0 1 
                                0 
                              1 0 1 
                            2 1 0 1 2 
                          3 2 1 0 1 2 3 
                        4 3 2 1 0 1 2 3 4 
                      5 4 3 2 1 0 1 2 3 4 5

View it as:-

                      * * * * * * * * * * *
                        * * * * * * * * * 
                          * * * * * * * 
                            * * * * * 
                              * * * 
                                * 
                              * * * 
                            * * * * * 
                          * * * * * * * 
                        * * * * * * * * * 
                      * * * * * * * * * * *

Given pattern is seen as first spaces and then numbers.The numbers are first in in decreasing order and then in increasing order in a row. Spaces first increases and then decreases in a row. So do the work of spaces first and then that of numbers.And then change variables accordingly for next iterations.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.