What is wrong with my code.why output is not correct

why no reply from any TA’s yet

package Questions;
import java.util.Scanner;
public class sudoku_solver {
public static void main(String[] args)
{
Scanner obj =new Scanner(System.in);
int[][] board=new int[10][10];
int n=obj.nextInt();

	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			board[i][j]=obj.nextInt();
		}
	}
	int l=board.length;
	if(solveSudoku(board,l))
	{
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<n;j++)
			{
				System.out.print(board[i][j]);
				System.out.print(" ");
			}
			System.out.println();

		}
	}
	else
	{
		System.out.println("no solution");
	}
}
public static boolean solveSudoku(int[][] board,int l)
{
	int row=-1;//where we will find our first empty cell
	int col=-1;
	boolean isEmpty=false;
	for(int i=0;i<l;i++)
	{
		for(int j=0;j<l;j++)
		{
			if(board[i][j]==0) // whether there is any empty cell
			{
				row=i;      
				col=j;
				isEmpty=true;// than set isEmpty to true
				break;
			}
		}
		if(isEmpty)// if you are having isEmpty value true that means now
			break;// you have to try to place a number .so break
	}
	if(!isEmpty) //if is empty value comes false that means your sudoku is solved.
	{
		return true;
	}

	for(int num=1;num<=9;num++)
	{
		if(isItSafe(board,row,col,num))// check whether num is safe to keep in row i and col j
		{
			board[row][col]=num;//if true place number
			if(solveSudoku(board,l))//call to function again to find another empty
			{
				return true;    
			}//space
			board[row][col]=0;
		}
	}
	return false;
}
public static boolean isItSafe(int[][] board,int row,int col, int num)
{
	for(int i=0;i<board.length;i++)// to check wether column is safe
	{
		if(board[i][col]==num)
		{
			return false;
		}

	}

	for(int i=0;i<board.length;i++)// to check whether row is safe
	{
		if(board[row][i]==num)
		{
			return false;
		}
	}
	int sqrt =(int)Math.sqrt(board.length);// to check wether box is safe
	int rowstart=row-row % sqrt;
	int colstart=col-col % sqrt;
	for(int i=rowstart;i<rowstart+sqrt;i++)
	{
		for(int j=colstart;j<colstart+sqrt;j++)
		{
			if(board[i][j]==num)
				return false;

		}
	}
	return true;
}

}

your code is absolutely correct…just a small change make the board of size nXn instead of 10X10

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.