icse-promo

Question

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:
1xdigit1 + 2xdigit2 + 3xdigit3 + 4xdigit4 + 5xdigit5 + 6xdigit6 + 7xdigit7 + 8xdigit8 + 9xdigit9 + 10xdigit10 is divisible by 11.
Example: For an ISBN 1401601499
Sum=1×1 + 2×4 + 3×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 integer.
(ii) If the ISBN is not a 10-digit integer, 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”. 

Share code with your friends

Share on whatsapp
Share on facebook
Share on twitter
Share on telegram

Code

				
					import java.util.Scanner;

public class program5 
{
	public static void main(String[] args)
	{
	    int i=0,sum=0,digit=0;
		Scanner sc = new Scanner(System.in);
		System.out.print("Enter ISBN code: ");
		long isbnLong = sc.nextLong();
		String isbn = Long.toString(isbnLong);
		if (isbn.length() != 10)
		{
			System.out.println("Ilegal ISBN");
		}
		else
		{
			sum = 0;
			for (i = 0; i < 10; i++)
			{
			    digit = Integer.parseInt(Character.toString(isbn.charAt(i)));
				sum = sum + (digit * (i + 1));
			}
			if (sum % 11 == 0)
			{
				System.out.println(isbn+" is a Legal ISBN");
			}
			else
			{
				System.out.println(isbn+" is not a legal ISBN");
			}
		}
	}
}


				
			

Coding Store

Leave a Reply

Your email address will not be published. Required fields are marked *