-
Notifications
You must be signed in to change notification settings - Fork 0
/
isbn.c
66 lines (53 loc) · 1.22 KB
/
isbn.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <stdio.h>
#include <stdbool.h>
bool isValid(char *);
int main()
{
char isbn[10];
printf("Enter an ISBN code: ");
gets(isbn);
if (isValid(isbn))
{
printf("Valid ISBN Code");
}
else
{
printf("Invalid ISBN Code");
}
return 0;
}
// Core Function to implement the Algorithm to check validity of the ISBN
bool isValid(char *isbn)
{
// the sum that must be divisible by 11
int sum = 0;
// sum = 1d1 + 2d2 + 3d3 + 4d4 + ... ndn, that n !!
int multiple = 1;
// from the right
isbn = isbn + 9;
for(int i = 1; i <= 10; i++)
{
//printf("%c\n", *isbn);
// X is 10
if (*isbn == 'X')
{
sum += multiple * 10;
//printf("10 multiplied !\n");
}
else
sum += multiple * (int)(*isbn - '0');
/*Character to corresponging integer conversion
with - '0'. Found it on Google (: */
//printf("%d\n", sum);
multiple = multiple + 1;
--isbn;
}
if (sum % 11 == 0)
{
return true;
}
else
{
return false;
}
}