Program to check for ISBN
Last Updated :
17 Feb, 2023
An ISBN (International Standard Book Number) is a 10 digit number that is used to identify a book.
The first nine digits of the ISBN number are used to represent the Title, Publisher and Group of the book and the last digit is used for checking whether ISBN is correct or not.
The first 9 digits of it, can take any value between 0 and 9, but the last digits, sometimes may take value equal to 10; this is done by writing it as ‘X’.
To verify an ISBN, calculate 10 times the first digit, plus 9 times the second digit, plus 8 times the third digit and so on until we add 1 time the last digit. If the final number leaves no remainder when divided by 11, the code is a valid ISBN.
Examples :
Input : 007462542X
Output : Valid
007462542X = 10*0 + 9*0 + 8*7 + 7*4 + 6*6 +
5*2 + 4*5 + 3*4 + 2*2 + 1*10 = 176
Since 55 leaves no remainder when divided
by 11, hence it is a valid ISBN.
Input : 0112112425
Output : Invalid
0112112425 = 10*0 + 9*1 + 8*1 + 7*2 + 6*1 +
5*1 + 4*1 + 3*4 + 2*2 + 1*5 = 71
Since 71 is not divisible by 11, given number
is not a valid ISBN.
Now, we design a program to accept a ten digit code from the user and then we will check whether a number is ISBN or not. Display an appropriate message.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
bool isValidISBN(string& isbn)
{
int n = isbn.length();
if (n != 10)
return false ;
int sum = 0;
for ( int i = 0; i < 9; i++)
{
int digit = isbn[i] - '0' ;
if (0 > digit || 9 < digit)
return false ;
sum += (digit * (10 - i));
}
char last = isbn[9];
if (last != 'X' && (last < '0' ||
last > '9' ))
return false ;
sum += ((last == 'X' ) ? 10 :
(last - '0' ));
return (sum % 11 == 0);
}
int main()
{
string isbn = "007462542X" ;
if (isValidISBN(isbn))
cout << "Valid" ;
else
cout << "Invalid" ;
return 0;
}
|
Java
class GFG
{
static boolean isValidISBN(String isbn)
{
int n = isbn.length();
if (n != 10 )
return false ;
int sum = 0 ;
for ( int i = 0 ; i < 9 ; i++)
{
int digit = isbn.charAt(i) - '0' ;
if ( 0 > digit || 9 < digit)
return false ;
sum += (digit * ( 10 - i));
}
char last = isbn.charAt( 9 );
if (last != 'X' && (last < '0' ||
last > '9' ))
return false ;
sum += ((last == 'X' ) ? 10 : (last - '0' ));
return (sum % 11 == 0 );
}
public static void main(String[] args)
{
String isbn = "007462542X" ;
if (isValidISBN(isbn))
System.out.print( "Valid" );
else
System.out.print( "Invalid" );
}
}
|
Python3
def isValidISBN(isbn):
if len (isbn) ! = 10 :
return False
_sum = 0
for i in range ( 9 ):
if 0 < = int (isbn[i]) < = 9 :
_sum + = int (isbn[i]) * ( 10 - i)
else :
return False
if (isbn[ 9 ] ! = 'X' and
0 < = int (isbn[ 9 ]) < = 9 ):
return False
_sum + = 10 if isbn[ 9 ] = = 'X' else int (isbn[ 9 ])
return (_sum % 11 = = 0 )
isbn = "007462542X"
if isValidISBN(isbn):
print ( 'Valid' )
else :
print ( "Invalid" )
|
C#
using System;
class GFG {
static bool isValidISBN( string isbn)
{
int n = isbn.Length;
if (n != 10)
return false ;
int sum = 0;
for ( int i = 0; i < 9; i++)
{
int digit = isbn[i] - '0' ;
if (0 > digit || 9 < digit)
return false ;
sum += (digit * (10 - i));
}
char last = isbn[9];
if (last != 'X' && (last < '0'
|| last > '9' ))
return false ;
sum += ((last == 'X' ) ? 10 :
(last - '0' ));
return (sum % 11 == 0);
}
public static void Main()
{
string isbn = "007462542X" ;
if (isValidISBN(isbn))
Console.WriteLine( "Valid" );
else
Console.WriteLine( "Invalid" );
}
}
|
PHP
<?php
function isValidISBN( $isbn )
{
$n = strlen ( $isbn );
if ( $n != 10)
return -1;
$sum = 0;
for ( $i = 0; $i < 9; $i ++)
{
$digit = $isbn [ $i ] - '0' ;
if (0 > $digit || 9 < $digit )
return -1;
$sum += ( $digit * (10 - $i ));
}
$last = $isbn [9];
if ( $last != 'X' && ( $last < '0' ||
$last > '9' ))
return -1;
$sum += (( $last == 'X' )
? 10 : ( $last - '0' ));
return ( $sum % 11 == 0);
}
$isbn = "007462542X" ;
if (isValidISBN( $isbn ))
echo "Valid" ;
else
echo "Invalid" ;
?>
|
Javascript
<script>
function isValidISBN(isbn)
{
let n = isbn.length;
if (n != 10)
return false ;
let sum = 0;
for (let i = 0; i < 9; i++)
{
let digit = isbn[i] - '0' ;
if (0 > digit || 9 < digit)
return false ;
sum += (digit * (10 - i));
}
let last = isbn[9];
if (last != 'X' && (last < '0' || last > '9' ))
return false ;
sum += ((last == 'X' ) ? 10 : (last - '0' ));
return (sum % 11 == 0);
}
let isbn = "007462542X" ;
if (isValidISBN(isbn))
document.write( "Valid" );
else
document.write( "Invalid" );
</script>
|
Time Complexity: O(1),as loop is running for constant number of times.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
The above code checks for ISBN 10. The recent version of ISBN is ISBN 13 (in 2005).
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...