Doubt in patterns


not able to solve it

Just think of it this way. You first need to print a line of n asterisks. Then for n - 2 lines you need to print asterisk followed by n - 2 spaces followed by an asterisk. Then simply print another line similar to the first line.

1 Like

hello. sorry for bothering.
i was going through your instructions but problem a problem arised
my pattern is coming like this
6



and my code is
import java.util.Scanner;

public class tgf {

public static void main(String[] args) {
	Scanner scn = new Scanner(System.in);
	int n = scn.nextInt();

	// row
	int row = 1;
	int nsp;
	int nst;
	while (row <= n) {
		if (row == 1 || row == n) {
			nsp = 0;
			nst = n;

		} else {
			nsp = n - 2;
			nst = 1;
		}

		// work for stars
		for (int cst = 1; cst <= nst; cst++) {
			System.out.print("*");
		}
		// work for space
		for (int csp = 1; csp <= nsp; csp++) {
			System.out.print(" ");
		}
		// work for stars

		for (int cst = 1; cst <= nst; cst++) {
			System.out.print("*");

		}
		// prep
		System.out.println();
		row = row + 1;

	}

}

}

Hey @gautamparti
you needed to add space after each * and space too
also manage the ending stars for row!=n or 1
this is your corrected code:


please mark your doubt as resolved and rate as well :slight_smile:

1 Like