My Java code isn't working here please check. It's working in Eclipse

package lecture2;

public class ans2 {

public static void main(String[] args) {
	// TODO Auto-generated method stub

	int n = 5;
	int nsp = 2 * n - 1;

	int nr = 2 * n + 1;
	int num = n;
	int a = 0;

	for (int i = 1; i <= nr; i++) {
		if (i > n + 1)
			a = nr - i + 1;
		else
			a = i;


		for (int cst = 1; cst <= a; cst++) {
			System.out.print(num);
			num--;
		}


		for (int csp = 1; csp <= nsp; csp++) {
			System.out.print(" ");
		}


		for (int cst = 1; cst <= a; cst++) {
			num++;
			if (num != 0)
				System.out.print(num);
		}


		if (i <= (nr) / 2) {
			nsp = nsp - 2;

		} else
			nsp = nsp + 2;

		System.out.println();
	}
}

}

@discobot Whenever you use Coding Blocks IDE your class name should be “Main” And there should be any package leture 2; in the code. Also you have to take input from the user don’t give hardcoded values to ‘n’ instead use scanner to scan that value from user. So corrected Code is :


public class Main {

public static void main(String[] args) {
	// TODO Auto-generated method stub

	Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
	int nsp = 2 * n - 1;

	int nr = 2 * n + 1;
	int num = n;
	int a = 0;

	for (int i = 1; i <= nr; i++) {
		if (i > n + 1)
			a = nr - i + 1;
		else
			a = i;


		for (int cst = 1; cst <= a; cst++) {
			System.out.print(num);
			num--;
		}


		for (int csp = 1; csp <= nsp; csp++) {
			System.out.print(" ");
		}


		for (int cst = 1; cst <= a; cst++) {
			num++;
			if (num != 0)
				System.out.print(num);
		}


		if (i <= (nr) / 2) {
			nsp = nsp - 2;

		} else
			nsp = nsp + 2;

		System.out.println();
	}
}
}

Hi! To find out what I can do, say @discobot display help.

Sir this code isn’t working too. Please help

@discobot Add one more space in every Syso Statement so that your code looks the same as in sample format.(Note even a single space can cause your answer to be wrong.)
Corrected Code is below :


import java.util.*;
public class Main {

public static void main(String[] args) {
	// TODO Auto-generated method stub

	Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
	int nsp = 2 * n - 1;

	int nr = 2 * n + 1;
	int num = n;
	int a = 0;

	for (int i = 1; i <= nr; i++) {
		if (i > n + 1)
			a = nr - i + 1;
		else
			a = i;


		for (int cst = 1; cst <= a; cst++) {
			System.out.print(num+" ");
			num--;
		}


		for (int csp = 1; csp <= nsp; csp++) {
			System.out.print("  ");
		}


		for (int cst = 1; cst <= a; cst++) {
			num++;
			if (num != 0)
				System.out.print(num+" ");
		}


		if (i <= (nr) / 2) {
			nsp = nsp - 2;

		} else
			nsp = nsp + 2;

		System.out.println();
	}
}
}

Hi! To find out what I can do, say @discobot display help.

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.