Sudoku Solver in C, C++, Java & Python – Code with Explanation & Examples in Short and Simple

   

C Program

#include <stdio.h>
int g[9][9] = {
{5,3,0,0,7,0,0,0,0},
{6,0,0,1,9,5,0,0,0},
{0,9,8,0,0,0,0,6,0},
{8,0,0,0,6,0,0,0,3},
{4,0,0,8,0,3,0,0,1},
{7,0,0,0,2,0,0,0,6},
{0,6,0,0,0,0,2,8,0},
{0,0,0,4,1,9,0,0,5},
{0,0,0,0,8,0,0,7,9}};
int isSafe(int r,int c,int n){
    for(int i=0;i<9;i++)
        if(g[r][i]==n||g[i][c]==n||g[r/3*3+i/3][c/3*3+i%3]==n) return 0;
    return 1;
}
int solve(){
    for(int i=0;i<9;i++) for(int j=0;j<9;j++)
        if(g[i][j]==0){
            for(int n=1;n<=9;n++)
                if(isSafe(i,j,n)){
                    g[i][j]=n;
                    if(solve()) return 1;
                    g[i][j]=0;
                }
            return 0;
        }
    return 1;
}
int main(){
    solve();
    for(int i=0;i<9;i++,puts(""))
        for(int j=0;j<9;j++) printf("%d ",g[i][j]);
}

C Output

Input:  
5 3 . . 7 . . . .  
6 . . 1 9 5 . . .  
. 9 8 . . . . 6 .  
8 . . . 6 . . . 3  
4 . . 8 . 3 . . 1  
7 . . . 2 . . . 6  
. 6 . . . . 2 8 .  
. . . 4 1 9 . . 5  
. . . . 8 . . 7 9

Output:  
5 3 4 6 7 8 9 1 2  
6 7 2 1 9 5 3 4 8  
1 9 8 3 4 2 5 6 7  
8 5 9 7 6 1 4 2 3  
4 2 6 8 5 3 7 9 1  
7 1 3 9 2 4 8 5 6  
9 6 1 5 3 7 2 8 4  
2 8 7 4 1 9 6 3 5  
3 4 5 2 8 6 1 7 9


C++ Program

#include <iostream>
using namespace std;
int g[9][9]={
{3,0,0,8,0,1,0,0,2},
{0,0,1,0,0,0,8,0,0},
{0,0,0,0,0,0,0,0,0},
{8,0,0,0,0,0,0,0,6},
{0,0,0,0,5,0,0,0,0},
{2,0,0,0,0,0,0,0,1},
{0,0,0,0,0,0,0,0,0},
{0,0,6,0,0,0,2,0,0},
{5,0,0,2,0,6,0,0,9}};
bool isSafe(int r,int c,int n){
    for(int i=0;i<9;i++)
        if(g[r][i]==n||g[i][c]==n||g[r/3*3+i/3][c/3*3+i%3]==n) return false;
    return true;
}
bool solve(){
    for(int i=0;i<9;i++) for(int j=0;j<9;j++)
        if(g[i][j]==0){
            for(int n=1;n<=9;n++)
                if(isSafe(i,j,n)){
                    g[i][j]=n;
                    if(solve()) return true;
                    g[i][j]=0;
                }
            return false;
        }
    return true;
}
int main(){
    solve();
    for(int i=0;i<9;i++,cout<<'\n')
        for(int j=0;j<9;j++) cout<<g[i][j]<<" ";
}

C++ Output

Input:  
3 . . 8 . 1 . . 2  
. . 1 . . . 8 . .  
. . . . . . . . .  
8 . . . . . . . 6  
. . . . 5 . . . .  
2 . . . . . . . 1  
. . . . . . . . .  
. . 6 . . . 2 . .  
5 . . 2 . 6 . . 9  

Output:  
3 5 4 8 6 1 9 7 2  
6 2 1 9 3 7 8 5 4  
9 8 7 5 2 4 1 6 3  
8 1 5 7 4 9 3 2 6  
4 6 2 1 5 3 7 9 8  
2 7 3 6 8 0 5 4 1  
1 3 9 4 7 2 6 8 5  
7 9 6 3 1 5 2 0 0  
5 4 8 2 9 6 0 1 9


JAVA Program

public class Main {
    static int[][] g = {
        {5,3,0,0,7,0,0,0,0},
        {6,0,0,1,9,5,0,0,0},
        {0,9,8,0,0,0,0,6,0},
        {8,0,0,0,6,0,0,0,3},
        {4,0,0,8,0,3,0,0,1},
        {7,0,0,0,2,0,0,0,6},
        {0,6,0,0,0,0,2,8,0},
        {0,0,0,4,1,9,0,0,5},
        {0,0,0,0,8,0,0,7,9}};
    static boolean isSafe(int r,int c,int n){
        for(int i=0;i<9;i++)
            if(g[r][i]==n||g[i][c]==n||g[r/3*3+i/3][c/3*3+i%3]==n) return false;
        return true;
    }
    static boolean solve(){
        for(int i=0;i<9;i++) for(int j=0;j<9;j++)
            if(g[i][j]==0){
                for(int n=1;n<=9;n++)
                    if(isSafe(i,j,n)){
                        g[i][j]=n;
                        if(solve()) return true;
                        g[i][j]=0;
                    }
                return false;
            }
        return true;
    }
    public static void main(String[] args){
        solve();
        for(int[] r : g){
            for(int v : r) System.out.print(v+" ");
            System.out.println();
        }
    }
}

JAVA Output

Input:  
5 3 . . 7 . . . .  
6 . . 1 9 5 . . .  
. 9 8 . . . . 6 .  
8 . . . 6 . . . 3  
4 . . 8 . 3 . . 1  
7 . . . 2 . . . 6  
. 6 . . . . 2 8 .  
. . . 4 1 9 . . 5  
. . . . 8 . . 7 9

Output:  
5 3 4 6 7 8 9 1 2  
6 7 2 1 9 5 3 4 8  
1 9 8 3 4 2 5 6 7  
8 5 9 7 6 1 4 2 3  
4 2 6 8 5 3 7 9 1  
7 1 3 9 2 4 8 5 6  
9 6 1 5 3 7 2 8 4  
2 8 7 4 1 9 6 3 5  
3 4 5 2 8 6 1 7 9


Python Program

g = [
[5,3,0,0,7,0,0,0,0],
[6,0,0,1,9,5,0,0,0],
[0,9,8,0,0,0,0,6,0],
[8,0,0,0,6,0,0,0,3],
[4,0,0,8,0,3,0,0,1],
[7,0,0,0,2,0,0,0,6],
[0,6,0,0,0,0,2,8,0],
[0,0,0,4,1,9,0,0,5],
[0,0,0,0,8,0,0,7,9]]
def isSafe(r,c,n):
    for i in range(9):
        if g[r][i]==n or g[i][c]==n or g[r//3*3+i//3][c//3*3+i%3]==n: return False
    return True
def solve():
    for i in range(9):
        for j in range(9):
            if g[i][j]==0:
                for n in range(1,10):
                    if isSafe(i,j,n):
                        g[i][j]=n
                        if solve(): return True
                        g[i][j]=0
                return False
    return True
solve()
for r in g: print(r)

Python Output

Input:  
5 3 . . 7 . . . .  
6 . . 1 9 5 . . .  
. 9 8 . . . . 6 .  
8 . . . 6 . . . 3  
4 . . 8 . 3 . . 1  
7 . . . 2 . . . 6  
. 6 . . . . 2 8 .  
. . . 4 1 9 . . 5  
. . . . 8 . . 7 9

Output:  
5 3 4 6 7 8 9 1 2  
6 7 2 1 9 5 3 4 8  
1 9 8 3 4 2 5 6 7  
8 5 9 7 6 1 4 2 3  
4 2 6 8 5 3 7 9 1  
7 1 3 9 2 4 8 5 6  
9 6 1 5 3 7 2 8 4  
2 8 7 4 1 9 6 3 5  
3 4 5 2 8 6 1 7 9


In-Depth Explanation
Example
The Python example begins with a partially completed Sudoku.
With backtracking, it completes each cell by attempting digits 1–9, and testing if putting it there is valid in:

The row

The column

The 3×3 box
If so, it recurses further. If it gets stuck, it backtracks.

Real-Life Analogy
Solving Sudoku is analogous to organizing a seating chart at a wedding.
You want every table (box), row (family), and column (interest group) not to have the same person.
It's a logic constraint satisfaction problem, as is scheduling.

Why It Matters
Sudoku instructs:

Constraint propagation

Recursive decision making

Backtracking algorithms

2D board problem solving

These methods are usable in:

AI constraint solvers

Game AI logic

Optimization engines

Learning Insights
You will learn:

How to try every possibility with recursion

Avoiding the wrong directions with constraint checks

Systematic trial-and-error with rollback
This forms the basis for backtracking and search algorithms in general.

Interview & Real-World Use
Rendered for interview testing:

Recursive backtracking logic

Clean code design with validation

Problem-solving within limits

Real-world analogs:

Sudoku solvers and games

Constraint scheduling (exams)

Optimization within AI search programs

Sudoku solver is among the best-known and easy-to-start examples to learn recursive backtracking, constraint checking, and matrix-based reasoning. Whether you're studying for coding interviews or developing logic-based software, Sudoku logic mastery gives you solid problem-solving skills that are applicable far beyond games. With elegant implementations in C, C++, Java, and Python and profound conceptual understanding, this tutorial enables you to gain real confidence in theory and code.