FOR FREE CONTENT

ISBN Number

Back to Programming

Description

ISBN stands for International Standard Book Number. It is a unique number which is used to identify any book and this number is printed on every book. This number should be a 10 digit number and it is legal iff 1xdigit1 + 2xdigit2 + 3xdigit3 + 4xdigit4 + 5xdigit5 + 6xdigit6 + 7xdigit7 + 8xdigit8 + 9xdigit9 + 10xdigit10 is divisible by 11

 

For example if an ISBN number is 1401601499. Then the sum is calculated as: 1×1 + 2×4 + 3×0 + 4×1 + 5×6 + 6×0 + 7×1 + 8×4 + 9×9 + 10×9 = 253 and this is divisible by 11.

If the number is 12345, then the number of digits is 5, therefore, this number is not an ISBN number. Similarly, if the number is 1034621212 then the number of digits is 10. The sum is: 1×1 + 0×2 + 3×3 + 4×4 + 6×5 + 2×6 + 1×7 + 2×8 + 1×9 + 2×10 = 120 which is not divisible by 11. Therefore, this is not an ISBN number

Algorithm

INPUT: A number
OUTPUT: Whether the number is a legal ISBN number or not.
PROCESS:
Step 1: [Taking the input]
	Read n [The number to check]
Step 2: [Checking for ISBN number]
Set d<-0
Set s<-0
[Counting the number of digits of the number]
	Set tmp<-n
	While tmp>0 repeat
		Set d<-d+1
		Set tmp<-tmp/10
	[End of ‘while’ loop]
	[Calculating the sum of the product of each digit with their position]
	If d=10 then
		While n>0 repeat
			Set s<-s+(n mod 10)×d
			Set d<-d-1
			Set n<-n/10
		[End of ‘while’ loop]
		[Checking if the ISBN is legal or not]
		If s mod 11=0 then
			Print "The ISBN number is legal"
		Else
			Print "The ISBN number is not legal"
		[End of ‘if-else’]
	[End of ‘if’]
	Else
		Print "The ISBN number is not legal"
	[End of ‘Else’]
Step 3: Stop.

Code

Time Complexity:

while(tmp>0)------------------------------------------- O(m)

                {              d++;

                                tmp/=10;  }

                //calculating the sum of the product of each digit

                //with their position

                if(d==10)-------------------------------------------------- O(1)

                {

                                while(n>0)----------------------------------- O(m)

                                {              s=s+(n%10)*d;

                                                d--;

                                                n/=10;    }

                                //checking if the ISBN is legal or not

                                if(s%11==0)---------------------------------- O(1)

                                                printf("The ISBN number is legal");

                                else

                                                printf("The ISBN number is not legal");

                }

                else

                                printf("The ISBN number is not legal");

The time complexity of this program is O(m) where ‘m’ is the number of digits of the input number.

 

Space Complexity:

The space complexity of this program is O(1) as it requires a constant number of memory spaces for any given number.