Pages

Tuesday, 29 November 2016

An ISBN (International Standard Book Number) is a ten digit code which uniquely identifies a book.

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 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:

Input Specifications.
The first line contains - ISBN number
Output Specification :
Print “N is a valid ISBN number” or “ N is not valid ISBN number” or “N is an Invalid Input” 
Sample Input:
007462542X
Sample output
007462542X is a valid ISBN number


Source Code:-

import java.util.*;

class Test{
    public static void main(String []args){
        Scanner s=new Scanner(System.in);
        String isbn=s.next().toUpperCase();
        int sum=0,i=10;
        try{
        if(isbn.length()==10){
            for(char c:isbn.toCharArray()){
                if(c=='X'){
                    sum+=i*10;
                }
                else if(c>='0' && c<='9'){
                    sum+=i*Character.getNumericValue(c);
                }
                else{
                    throw new Exception();
                }
                i--;
            }
            if(sum%11==0){
                System.out.println(isbn+" is a valid ISBN number");
            }
            else{
                System.out.println(isbn+" is not a valid ISBN number");
            }
        }
        else{
            System.out.println(isbn+" is an Invalid input");
        }
        }
        catch(Exception e){
            System.out.println(isbn+" is an Invalid input");
        }
    }
}

No comments:

Post a Comment