Please check my code it is right but doing problem in the lower triangle (pattern hourglass)

my code :

Scanner s = new Scanner(System.in);
int n = s.nextInt();

	// row
	int row = 1;
	int nr = 2 * n + 1;
	// work
	int nsp = 0;
	int nst = nr;
	// value
	int val;
	while (row <= nr) {
		// value
		val = n + 1 - row;
		// space work
		for (int csp = 1; csp <= nsp; csp++) {
			System.out.print("-");
		}
		// number work
		for (int cst = 1; cst <= nst; cst++) {
			System.out.print(val);
			if (row <= nr / 2 && cst <= nst / 2) {
				val--;
			} else if (row <= nr / 2 && cst >= nst / 2) {
				val++;
			} else if (row > nr / 2 && cst <= nst / 2) {
				val++;
			} else if (row > nr / 2 && cst >= nst / 2) {
				val++;
			}

		}

		// preparation
		if (row <= nr / 2) {
			nsp++;
			nst = nst - 2;

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

		}
		System.out.println();
		row++;
	}

Hii Nipun,
First take absolute of value of val variable as when row is 7 the value of val will be 5 + 1 - 7 = -1 but for 7th row value of val should be 1. For finding absolute value you can use Math.abs() function. Also if cst <= nst/2, no matter what row is value of val variable will always decrease by 1. And if cst > nst/2, no matter what row is value of val variable will always increase by 1.

i have done it but in different way

my code:

Scanner s = new Scanner(System.in);
int n = s.nextInt();

	// row
	int row = 1;
	int nr = 2 * n + 1;
	// work
	int nst = 2 * n + 1;
	int nsp = 0;
	// value
	int val;
	while (row <= nr) {

		// work space
		for (int csp = 1; csp <= nsp; csp++) {
			System.out.print("-");
		}
		// value
		if (row <= nr / 2 + 1) {
			val = n + 1 - row;
		} else {
			val = row + n - nr;
		}
		// work stars
		for (int cst = 1; cst <= nst; cst++) {
			System.out.print(val);
			if (cst <= nst / 2) {
				val--;
			} else {
				val++;
			}

		}

		// preparation
		if (row <= nr / 2) {
			nsp++;
			nst = nst - 2;
		} else {
			nsp--;
			nst = nst + 2;
		}
		System.out.println();
		row++;
	}

Hi Nipun

I have made the changes to your code. It should work fine now.

import java.util.*;

class Main {

public static void main(String args[]) {
	Scanner s = new Scanner(System.in);
	int n = s.nextInt();
	// row
	int row = 1;
	int nr = 2 * n + 1;
	// work
	int nst = 2 * n + 1;
	int nsp = 0;
	// value
	int val;
	while (row <= nr) {

		// work space
		for (int csp = 1; csp <= nsp; csp++) {
			System.out.print("  ");
		}
		// value
		if (row <= nr / 2 + 1) {
			val = n + 1 - row;
		} else {
			val = row + n - nr;
		}
		// work stars
		for (int cst = 1; cst <= nst; cst++) {
			System.out.print(" "+ val );
			if (cst <= nst / 2) {
				val--;
			} else {
				val++;
			}

		}

		// preparation
		if (row <= nr / 2) {
			nsp++;
			nst = nst - 2;
		} else {
			nsp--;
			nst = nst + 2;
		}
		System.out.println();
		row++;
	}
}

}

Hi Nipun

As you are not responding to this thread, I am marking your doubt as Resolved for now. Re-open it if required.

Please mark your doubts as resolved in your course’s “ Ask Doubt ” section, when your doubt is resolved.