Friday 25 October 2013

ISBN

/*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 whehter 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*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 divisible by 11, hence it is 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 1
INPUT CODE : 0231428031
OUTPUT : SUM = 122
LEAVES REMAINDER - INVALID ISBN CODE*/


import java.io.*;
class ISBN
{
 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
   String str;
   int digit,c=1,x=0,i,len;
   char ch;
   public void show()throws Exception
   {
    System.out.println("Enter the ISBN code:");
    str=br.readLine();
    len=str.length();
    if(len!=10)
    {
     System.out.println("Invalid input.");
    return;
    }
   for(i=len-1;i>=0;i--)
   {
     ch=str.charAt(i);
     if(ch=='X')
     digit=10;
     else
     digit=ch-48;
     x=x+digit*c;
     c++;
     }
     System.out.println("\nSum="+x);
     if(x%11==0)
     System.out.println("Leaves no remainder - valid ISBN code.");
     else
     System.out.println("Leaves remainder - invalid ISBN code.");
     }
     public static void main(String args[]) throws Exception
     {
      ISBN ob=new ISBN();
      ob.show();
      }
      }

No comments:

Post a Comment