I got the code but when I use my if conditions for Numbers Work 2 inside for loop for numbers (2) I get the wrong code but I don t get why that should happen as I am simply specifying condition for when to use condition 1 and when to use condition 2 in for loop.
When I write the same if condition before for loop it works perfectly fine .
I have commented my if condition before for loop and written it inside for loop so you get what I m asking…
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
int nst = 1;
int nsp = 2*n - 1;
int row = 1;
int rows = 2*n + 1;
while(row <= rows){
int val = n;
// Numbers Work
for(int cst = 1; cst <= nst; cst++){
System.out.print(val + " ");
val--;
}
// Spaces Work
for(int csp = 1; csp <= nsp; csp++){
System.out.print(" ");
}
// int cst = 1;
// if(row == n + 1) {
// val+=2;
// cst = 2;
// } else {
// val++;
// cst = 1;
// }
// Numbers Work 2
for(int cst = 1; cst <= nst; cst++) {
if(row == n + 1) {
val+=2;
cst = 2;
} else {
val++;
cst = 1;
}
System.out.print(val + " ");
val++;
}
// criteria
if(row <= n){
nst++;
nsp-=2;
} else {
nst--;
nsp+=2;
}
row++;
System.out.println();
}