First row not working properly?

package PatternPrograms;

import java.util.Scanner;

public class Pattern10 {

public static void main(String[] args) {

	System.out.println("Pattern 10 ");

	// taking values from user
	Scanner scan = new Scanner(System.in);
	int n = scan.nextInt();

	// rows
	int rows = 1;
	int nst = 1;
	int nsp = 2 * n - 3;
	while (rows <= n) {
		// for stars
		for (int cst=1; cst <= nst; cst++) {
			System.out.print("*");
		}

		// for space
		for (int csp = 1; csp <= nsp; csp++) {
			System.out.print(" ");
		}
		// for stars
		int cst = 1;
		if (rows == n) {
			cst = 2;
		}

		for (; cst <= nst; cst++) {
			System.out.print("*");
		}
		// preparation
		System.out.print("\n ");

		nst++;
		nsp = nsp - 2;
		rows++;

	}

}

}

@Nitya_Somani here you go, just extra space added while changing to new line.


Don’t forget to mark ya doubt resolved and rate full if there is any query feel free to ask.
Happy coding!

1 Like