I am not getting the desired output

import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int nst=1;
int row=1;
while(row<=n){
for(int cst=1;cst<=nst;cst++){
if(row%2>0 && cst>1 && cst<n){
System.out.print(β€œ0”);
}
else {
System.out.print(row);
}
}
System.out.println();
nst++;
row++;
}
}
}
this question is manmohan loves pattern-2

Please share your code using the online IDE (https://ide.codingblocks.com). It’ll be easier to debug it that way.

You have a logical error in the if condition.
We need to place 0s when cst is not at the boundary of the pyramid
So the if statement should be
if(cst>1 && cst<nst){
// print(0)
}

Also, check the relation between the row number and variable row
2nd row starts with 1, 3rd row starts with 2, 4th row starts with 3, 5th row starts with 4 and so on…
Therefore in the else block we need to print row-1 rather than row. (Will have to handle the case of 1st row explicitly).

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.