An ISBN ( International Standard Book Number) is a ten digit code which uniquely identifies a book. The first nine digits represent the Gro...

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. 02011003311 = 10 x 0 + 9 x 2 + 8 x 0 + 7 x 1 + 6 x 1 + 5 x 0 + 4 x 3 + 3 x 3 + 2 x 1 + 1 x 1 = 55 Since 55 leaves no remainder when divisible by 11, hence it is a valid ISBN.
  2. 007462542X = 10 x 0 + 9 x 0 + 8 x 7 + 7 x 4 + 6 x 6 + 5 x 2 + 4 x 5 + 3 x 4 + 2 x 2 + 1 x 10 = 176 Since 176 leaves no remainder when divided by 11, hence it is a valid ISBN.

  3. 0112112425 = 10 x 0 + 9 x 1 + 8 x 1 + 7 x 2 + 6 x 1 + 5 x 1 + 4 x 1 + 3 x 4 + 2 x 2 + 1 x 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 inout, display an appropriate message. Verify the code for its validity in the format specified below:

Test your program with 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

import java.io.*;

class ISCPrac2013Q1{

public static void main(String args[])throws IOException{

BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));

System.out.println("INPUT CODE: ");
String isbn = br.readLine();

int l = isbn.length();
int i, t, ctr, s;
char ch;

if(l!= 10){

System.out.println("INVALID INPUT");

}else{
//PROCEED WITH COMPUTATION ONLY IF ISBN CODE CONTAINS 10 DIGITS
ctr = 10;
s = 0;

for(i=0;i < l;i++){

ch = isbn.charAt(i);

if(ch == 'X') //X REPRESENTS 10 IN ISBN CODE
    t = 10;
else
    t = ch - 48; //NUMERIC VALUE OF THE ASCII CODE OF DIGIT

    s= s + ctr*t; //PERFORM COMPUTATION

    ctr--;
}//END OF FOR LOOP

//IF SUM IS DIVISIBLE BY 11, IT IS A VALID ISBN CODE
if(s%11 == 0){
    System.out.println("SUM = "+s);
    System.out.println("LEAVES NO REMAINDER - VALID ISBN CODE");
}else{
    System.out.println("SUM = "+s);
    System.out.println("LEAVES REMAINDER - INVALID ISBN CODE");
}
}//END OF OUTERMOST ELSE

}//END OF MAIN
}//END OF CLASS

// EXAMPLE 1
// INPUT CODE: 
// 0201103311
// SUM = 55
// LEAVES NO REMAINDER - VALID ISBN CODE

//EXAMPLE 2
// INPUT CODE: 
// 0231428031
// SUM = 122
// LEAVES REMAINDER - INVALID ISBN CODE
// 

Have something to say? Log in to comment on this post.

0 comments