ISBN No.

An ISBN (International Standard Book Number) is a ten digit code which uniquely identifies a book.
The first nine digits represent the Group, Publisher and Title of the book and the last digit is used to check whether ISBN is correct or not.
Each of the first nine digits of the code can take a value between 0 and 9. Sometimes it is necessary to make the last digit equal to ten; this is done by writing the last digit of the code as X.
To verify an ISBN, calculate 10 times the first digit, plus 9 times the second digit, plus 8 times the third 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.
For Example:
1. 0201103311 = 10*0 + 9*2 + 8*0 + 7*1 + 6*1 + 5*0 + 4*3 + 3*3 + 2*1 + 1*1 = 55
Since 55 leaves no remainder when divided by 11, hence it is a valid ISBN.
2. 007462542X = 10*0 + 9*0 + 8*7 + 7*4 + 6*6 + 5*2 + 4*5 + 3*4 + 2*2 + 1*10 = 176
Since 176 leaves no remainder when divided by 11, hence it is a valid ISBN.
3. 0112112425 = 10*0 + 9*1 + 8*1 + 7*2 + 6*1 + 5*1 + 4*1 + 3*4 + 2*2 + 1*5 = 71
Since 71 leaves no remainder when divided by 11, hence it is not a valid ISBN.
Design a program to accept a ten digit code from the user. For an invalid input, display an appropriate message. Verify the code for its validity in the format specified below:
Test your program with the sample data and some random data:
Example 1
INPUT CODE: 0201530821
OUTPUT : SUM = 99
LEAVES NO REMAINDER – VALID ISBN CODE
Example 2
INPUT CODE: 035680324
OUTPUT : INVALID INPUT
Example 3
INPUT CODE: 0231428031
OUTPUT : SUM = 122
LEAVES REMAINDER – INVALID ISBN CODE


/**
* The class ISBN_ISC2013 inputs a 10 digit code and checks whether it is a valid ISBN code or not
* @Question Year : ISC Practical 2013 Question 1
*/
import java.io.*;
class ISBN_ISC2013
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a 10 digit code : ");
String s=br.readLine();
int len=s.length();
if(len!=10)
System.out.println("Output : Invalid Input");
else
{
char ch;
int dig=0, sum=0, k=10;
for(int i=0; i<len; i++)
{
ch=s.charAt(i);
if(ch=='X')
    dig=10;
else
    dig=ch-48;
sum=sum+dig*k;
k--;
}
/*Alternate Code which can be used instead of the above code
String ch;
int dig=0, sum=0, k=10;
for(int i=0; i<len; i++)
{
ch=Character.toString(s.charAt(i));
if(ch.equalsIgnoreCase("X"))
    dig=10;
else
    dig=Integer.parseInt(ch);
sum=sum+dig*k;
k--;
*/
System.out.println("Output : Sum = "+sum);
if(sum%11==0)
System.out.println("Leaves No Remainder - Valid ISBN Code");
else
System.out.println("Leaves Remainder - Invalid ISBN Code");
}
}
}




Output:

1. Enter a 10 digit code : 0201530821
Output : Sum = 99
Leaves No Remainder – Valid ISBN Code
2. Enter a 10 digit code : 035680324
Output : Invalid Input

No comments:

Post a Comment