Sudoku Checker in C Programming

In this tutorial we are going to talk about how to build a sudoku checker in C Programming. For the simplicity of our problem we took a 4×4 sudoku puzzle. Probably most of you know that Sudoku is a logic based, combinatorial, number replacement puzzle game. In a 4×4 sudoku puzzle there are four 2×2 sub regions. You have to fill all the blank places using integer from 1 ~ 4. The same single integer can not appear twice in the same row, column or in any of the four 2×2 subregions of 4×4 playing board. Here, our aim is to check the sudoku puzzle is correct or not once you finished filling all the blank places on the playing board. So we will check the sudoku board and find out whether it’s correct or not.

Now let’s have a look at our source code of Sudoku Checker in C Programming.

#include <stdio.h>

void sudoku_checker(int N ,int a[]){
    int i,j,k;
    int count=0;
    
    /* This for loop calculates the sum of each rows */
    /* and checks whether it's equal to (1+2+3+4) 10 or not */
    for (i = 0; i < N*N; i = i+4) {
    	int sumRow = 0;
    	for (j = i; j < i+4; j++) {
            sumRow = sumRow + a[j];
    	}
    	if (sumRow != 10) {
            	count++;
        }
    }
    
    /* This for loop calculates the sum of each columns */
    /* and checks whether it's equal to (1+2+3+4) 10 or not */
    for (i = 0; i < N; i++) {
    	int sumCol = 0;
    	for (j = i; j < N*N; j = j+4) {
            sumCol = sumCol + a[j];
    	}
    	if (sumCol != 10) {
            	count++;
        }
    }
	
	printf("Mistakes: %d\n", count);
	if(count==0)
		printf("OK\n");
	else
		printf("Incorrect\n");
}

int main(void){
	int a[]={2,1,3,4,4,3,2,1,1,3,4,2,2,4,3,1};
	int N = 4;

	for (int i = 0; i < N*N; i = i+4) {
		for (int j = i; j < i+4; j++) {
			printf("  %d  ", a[j]);
		}
		printf("\n");
	}

	sudoku_checker(N,a);

	return 0;
}

At first have a look at the structure of how to input value in the array.

sudoku checker input structure
sudoku checker input structure

Now let’s have a look at the input/output example once we run the code above.

Sudoku Checker
Sudoku Checker

On the above input/output example we can see that the sudoku we tried to fill is incorrect. Now let’s have a look at an input/output example of a correct one.

if we use the following array of input in line 35 then we can see how our program works for a correct solution.

int a[]={3,1,4,2,4,2,3,1,2,4,1,3,1,3,2,4};

Let’s have a look at a correct solution.

Sudoku Checker Correct
Sudoku Checker Correct

If you have any questions feel free to ask me in the comment section.

Books you should read :

Introduction to Algorithms, 3rd Edition (MIT Press)
Algorithms (4th Edition)

About MD Mosleh Uddin

Trying to create an interesting and comfortable environment for the beginners of different programming languages and web design.

5 thoughts on “Sudoku Checker in C Programming”

  1. actually i have made this by my own but i am facing some problem
    i want to get the value for sudoku from the user not all but some specific valuues how to get them??
    pls reply sir fast!

    1. use scanf inside for loop to get values from user…
      if you need to get some specific values means, in this case just specify your constrains to user :)

  2. So you check the sum of the rows and columns to see if it is equal to 10. The same should applie to a 9×9 sudoku.
    1+2+…+10=45
    But let just think about the 4 x 4 sudoku.
    What if i have 3 1 4 2 in arow which should be correct OR i have : 3 3 2 2
    The sum = 10 but we have 2 numbers two times…

Leave a Reply

Your email address will not be published. Required fields are marked *

sixteen − 10 =