package new_pattern_practice;
import java.util.Scanner;
public class ques8 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
//initializing variables
int nsp=n-2;
int nsp2=0;
for(int rows=1; rows<=n; rows++) {
for(int csp2=1; csp2<=nsp2; csp2++) {
System.out.print(' ');
}
System.out.print('*');
for(int csp=1; csp<=nsp;csp++) {
System.out.print(' ');
}
if(n%2==0 && rows!=n/2) {
System.out.print('*');
}
if(n%2!=0 && rows!=(n+1)/2) {
System.out.print('*');
}
//prep
System.out.println();
if(rows<=n/2) {
nsp-=2;
nsp2+=1;
} else {
nsp+=2;
nsp2-=1;
}
}
scn.close();
}
}
It works if n is odd (5 in the question) but can’t get it to work if n is even.