# 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.

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

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.

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

#### Books you should read :

[amazon template=findsourcecode&asin=0262033844][amazon template=findsourcecode&asin=032157351X]

Post Tagged with , , ,

### 5 Responses so far.

1. meet mehta says:

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??

• yuvi says:

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. Shweta says:

You’ve only checked rows and columns. What about the 2×2 subregion, or a 3×3 grid, for an actual Sudoku soln.?

3. ManosG says:

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…

• ManosG says:

I made a typo, it is 1+2…+9 = 45

This site uses Akismet to reduce spam. Learn how your comment data is processed.