package PatternsQuestions;
import java.util.Scanner;
public class ques8 {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
int nst = 1;
int nsp = n-3;
double mid = Math.ceil((double)n/2);
for(int row = 1; row <= n; row++) {
// work
// made an if statement for the first and last
// row
if(row == 1 || row == n) {
for(int cst =1; cst <= nst; cst++) {
System.out.print("*");
}
for(int csp =1; csp<= nsp; csp++) {
System.out.print(" ");
}
for(int cst =1; cst <= nst; cst++) {
System.out.print("*");
}
// an if statement when row = n/2;
} else if (row==mid) {
for(int csp =1; csp<= n/2; csp++) {
System.out.print(" ");
}
for(int cst =1; cst <= nst; cst++) {
System.out.print("*");
}
// an else statement for rest of the cases
} else {
for(int csp =1; csp<= nsp; csp++) {
System.out.print(" ");
}
for(int cst =1; cst <= nst; cst++) {
System.out.print("*");
}
for(int csp =1; csp<= nsp; csp++) {
System.out.print(" ");
}
}
// preparation
System.out.println();
if(row<n/2) {
nsp--;
} else if (row == mid) {
nsp=n/2;
} else {
nsp++;
}
scn.close();
}
}}
Would really appreciate if you could give me a solution for this.
I’m confused if a this complicate approach is needed.