For input 5 the pattern is not getting printed properly but for 7 it is getting how to fix this?

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

	int rows=1;
	int nsp=0;
	int nst=n*2+1;
	int mnsp=n;
	int nsp2=n-1;
	int nst2=n/2;

	


	//upper half
	while(rows<=2*n+1)
	{
	
		if(rows<=(n*2+1)/2)
		{
		for(int csp=1;csp<=nsp;csp++)
		{
			System.out.print(" "+" ");
		}
		nsp++;
		
		for(int cst=1;cst<=nst;cst++)
		{
			System.out.print("*"+" ");
		}
		nst=nst-2;
		
		}
		
		else if(rows==((2*n+1)/2)+1)
		{
			for(int csp=1;csp<=mnsp;csp++)
			{
				System.out.print(" "+" ");
			}
			System.out.print("*"+" ");
		}
		
		else 
		{
			for(int csp=1;csp<=nsp2;csp++)
			{
				System.out.print(" "+" ");
			}
			nsp2--;
			
			for(int cst=1;cst<=nst2;cst++)
			{
				System.out.print("*"+" ");
			}
			nst2=nst2+2;
			
		}
		

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

Hey @Nitya_Somani
int nst2 =3; // in every case

1 Like