My code is working fine but is causing trouble with even nos, can you please review it
Pattern 20 - Even nos input causing trouble
package pattern_practice_questions;
import java.util.Scanner;
public class Pattern20 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int nsp1 = n / 2;
int nsp2 = 0;
int rows = 1;
while(rows<=n){
for(int csp = 1;csp<=nsp1;csp++){
System.out.print(" ");
}
System.out.print("*");
for(int csp = 1;csp<=nsp2;csp++){
System.out.print(" ");
}
if(rows!=1 && rows!=n){
System.out.print("*");
}
System.out.println();
if (rows<=n/2){
nsp1--;
nsp2+=2;
}else{
nsp1++;
nsp2-=2;
}
rows++;
}
}
}
for such patterns, you don’t need to code for even numbers. Because there is no suitable pattern for even nos.