I have done number pattern 33 , but i do not completely understand it

please check my code and tell if my approach is correct and not too much complicated.

my code:

Scanner s = new Scanner(System.in);
int n = s.nextInt();

	// row
	int row = 1;

	// Ques 33:
	// n = 10
	// - - - - - - - - - 0
	// - - - - - - - - 9 0 9
	// - - - - - - - 8 9 0 9 8
	// - - - - - - 7 8 9 0 9 8 7
	// - - - - - 6 7 8 9 0 9 8 7 6
	// - - - - 5 6 7 8 9 0 9 8 7 6 5
	// - - - 4 5 6 7 8 9 0 9 8 7 6 5 4
	// - - 3 4 5 6 7 8 9 0 9 8 7 6 5 4 3
	// - 2 3 4 5 6 7 8 9 0 9 8 7 6 5 4 3 2
	// 1 2 3 4 5 6 7 8 9 0 9 8 7 6 5 4 3 2 1

	// work
	int nsp = n - 1;
	int nst = 1;
	int zero = 0;
	int val;
	while (row <= n) {
		val = n + 1 - row;
		int csp = 1;
		while (csp <= nsp) {
			System.out.print("- ");
			csp++;
		}

		int cst = 1;
		while (cst <= nst) {
			if (nsp + nst == n || csp + cst - 1 == n) // PROBLEM why i have
														// to do
														// csp+cst-1 and not
														// csp+cst+1
			{
				System.out.print(zero + " ");
			} else {

				System.out.print(val + " ");
				if (cst <= nst / 2) {
					val++;
				}
				if (cst >= nst / 2) {
					val--;
				}
			}
			cst++;
		}
		// preparation
		nst += 2;
		nsp--;
		System.out.println();
		row++;
	}

HI Nipun

You have used the right approach to solve the question.

Hi Nipun
As you are not responding to this thread, I am marking your doubt as Resolved for now. Re-open it if required.

Please mark your doubts as resolved in your course’s “ Ask Doubt ” section, when your doubt is resolved.

#include
using namespace std;
int main(){
int n;
cin>>n;
int nst = 1;
int nsp = n-1;
int rows=1;
int val;
while(rows<=n){
val = n-rows+1;
for(int csp = 1;csp<=nsp;csp++){
cout<<" “;
}
for(int cst = 1;cst<=nst;cst++){
if(cst == nst - rows+1){
cout<<“0”;
}
else{
cout<<val<<”";
if(cst<=nst/2){
val++;
}
if(cst>=nst/2){
val–;
}
}
}
cout<<endl;
nst+=2;
nsp-=1;
rows++;
}

return 0;

}
IS THIS ALSO CORRECT

#include
using namespace std;
int main(){
int n;
cin>>n;
int nst = 1;
int nsp = n-1;
int rows=1;
int val;
while(rows<=n){
val = n-rows+1;
for(int csp = 1;csp<=nsp;csp++){
cout<<" “;
}
for(int cst = 1;cst<=nst;cst++){
if(cst == nst - rows+1){
cout<<“0”;
}
else{
cout<<val<<”";
if(cst<=nst/2){
val++;
}
if(cst>=nst/2){
val–;
}
}
}
cout<<endl;
nst+=2;
nsp-=1;
rows++;
}

return 0;

}