luc070.c (1277B)
1 /* Write a program that receives a 10-digit ISBN number, computes the checksum (d1 + 2d2 + 3d3 + ... + 10d10), and reports whether the ISBN number is correct (sum divisible by 11). 2 */ 3 4 /* Let Us C, Chap- 15 (Strings), Qn No.: C(b) */ 5 6 /* This file is auto-generated by a bot. */ 7 /* This code is not compiled; it is for reference only. */ 8 9 10 #include <stdio.h> 11 #include <string.h> 12 #include <stdlib.h> 13 14 int main() 15 { 16 char isbn[15]; 17 int i, sum = 0, digit; 18 19 printf("Enter 10-digit ISBN number: "); 20 scanf("%s", isbn); 21 22 /* The formula given is: d1 + 2d2 + 3d3 + ... + 10d10 23 where di is the ith digit from the RIGHT. 24 25 If input is "007462542X" (Length 10): 26 isbn[0] is d10 (Weight 10) 27 isbn[1] is d9 (Weight 9) 28 ... 29 isbn[9] is d1 (Weight 1) 30 */ 31 32 for (i = 0; i < 10; i++) 33 { 34 // Handle 'X' which represents 10 in ISBN 35 if (isbn[i] == 'X' || isbn[i] == 'x') 36 digit = 10; 37 else 38 digit = isbn[i] - '0'; 39 40 // Weight is (10 - i) 41 sum += digit * (10 - i); 42 } 43 44 printf("Calculated Checksum: %d\n", sum); 45 46 if (sum % 11 == 0) 47 printf("The ISBN number is Correct.\n"); 48 else 49 printf("The ISBN number is Incorrect.\n"); 50 51 return 0; 52 }