The International Standard Book Number (ISBN) is a unique numeric book identifier which is printed on every book. The ISBN is based upon a 10-digit code. The ISBN is legal if 1*digit1 + 2*digit2 + 3*digit3 + 4*digit4 + 5*digit5 + 6*digit6 + 7*digit7 + 8*digit8 + 9*digit9 + 10*digit10 is divisible by 11.
Example: For an ISBN 1401601499
Sum=1*1 + 2*4 + 0*0 + 4*1 + 5*6 + 6*0 + 7*1 + 8*4 + 9*9 + 10*9 = 253 which is divisible by 11.
Write a program to:
(i) input the ISBN code as a 10-digit number
(ii) If the ISBN is not a 10-digit number, output the message “Illegal ISBN” and
terminate the program
(iii) If the number is 10-digit, extract the digits of the number and compute the sum
as explained above.
If the sum is divisible by 11, output the message “Legal ISBN”. If the sum is not divisible by 11, output the message “Illegal ISBN”.
Please solve the program for me!!
Answers
Program in JAVA
import java.util.*;
public class Main
{
static int cal(long n , int i)
{
int sum = 0;
while(n != 0)
{
int d = (int)(n % 10);
sum = sum + (d * i);
n = n / 10;
i--;
}
return sum;
}
public static void main(String[] args)
{
Scanner Sc = new Scanner(System.in);
String isbn;
System.out.print("Enter a ISBN : ");
isbn = Sc.next();
if(isbn.length() != 10)
{
System.out.print("Illegal ISBN");
}
else
{
int sum;
if(isbn.charAt(9) == 'x' || isbn.charAt(9) == 'X')
{
isbn = isbn.substring(0,9);
sum = cal(Long.parseLong(isbn) , 9)+ (10 * 10);
}
else
{
sum = cal(Long.parseLong(isbn) , 10);
}
if(sum % 11 == 0)
{
System.out.print("Legal ISBN");
}
else
{
System.out.print("Illegal ISBN");
}
}
}
}
Output 1:
Enter a ISBN : 1401601499
Legal ISBN
Output 2:
Enter a ISBN : 007462542X
Legal ISBN