In this why 4 th row is printing one extra star when taking input as 10 ?what is wrong in this code?

package PATTERNS;

import java.util.Scanner;

public class SwastikaPattern {

public static void main(String[] args)

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

	// 1st row
	System.out.print("*");
	// for space
	for (int cst = 1; cst <= (n - 3) / 2; cst++) {
		System.out.print(" ");
	}
	// for stars
	for (int cst = 1; cst <= (n + 1) / 2; cst++) {
		System.out.print("*");
	}

	System.out.println();

	// 2nd row and 3rd row
	for (int rows = 1; rows <= (n - 3) / 2; rows++) {
		System.out.print("*");
		for (int cst = 1; cst <= (n - 3) / 2; cst++) {
			System.out.print(" ");
		}
		System.out.println("*");
	}

	// 4th row
	for (int cst = 1; cst <= n; cst++) {
		System.out.print("*");
	}
	System.out.println();

	// 5th row and 6th row
	for (int rows = 1; rows <= (n - 3) / 2; rows++) {
		for (int cst = 1; cst < (n + 1) / 2; cst++) {
			System.out.print(" ");
		}
		System.out.print("*");
		for (int cst = 1; cst <= (n - 3) / 2; cst++) {
			System.out.print(" ");
		}
		System.out.println("*");
	}

	// 7 th row
	for (int cst = 1; cst <= (n + 1) / 2; cst++) {
		System.out.print("*");
	}
	for (int cst = 1; cst <= (n - 3) / 2; cst++) {
		System.out.print(" ");
	}
	System.out.print("*");
	System.out.println();

}

}

Hey @Nitya_Somani
code is fine :
Take as input N, an odd number (>=5) .

1 Like