Respuesta :
Answer:
Here is the JAVA program:
import java.util.Scanner; // gets input from user
public class ISBNChecksum
{
public static void main(String[] args) { //start of the main() function body
Scanner s = new Scanner(System.in); // creates Scanner class object
System.out.print("Enter a 9 digit integer:");
//prompts the user to enter a 9 digit integer
int num = s.nextInt(); // takes input integer values digits from the user
int a[] = new int[num]; //stores the value of integer into array a
int sum = 0; // for adding the values
for (int i = 2; i <= 10; i++) // loop starts from 2 and ends when i>10
{
int digit = num % 10; // extract right most digit
/* Multiply the first digit of input integer number by 10, the second by 9, the third by 8, so on, down to the ninth digit by 2 and add the values together */
sum = sum + i * digit;
num = num / 10; // remove the rightmost digit
}
System.out.print("The checksum digit is " + num);
int remainder = sum % 11; //computes the remainder
if(remainder == 1) //if remainder value is equal to 1
System.out.println("X"); // used as a checksum digit
else if (remainder == 0) // if remainder is 0
System.out.println("checksum is 0"); //checksum is 0
else //subtract the remainder from 11 to get the checksum digit.
System.out.println(11 - (remainder));}
}
Explanation:
The for loop basically computes the sum of each digit of the input number from right to left. First the right most digit is extracted using modulus operator and stores it into digit variable i.e. digit = num % 10;
Then the digit is multiplied by the value of i and added to the value in sum. The weighted sum of each digit is calculated this way. From right to left means that the first right most digit is multiplied by i = 2, the second rightmost digit is multiplied by 3 (i is incremented at every iteration) and so on and the loop terminates as the value of i gets greater of equal to 10. Then the sum is calculated for each result of product so the complete statement is sum = sum + i * digit; To remove the rightmost digit of a number at each iteration num = num / 10; is used which divides the number by 10. For example if the number is 1234 then 1234/10 gives 4 which is the rightmost digit.
Next the remainder of the computed sum is calculated by dividing the value stored in sum by 11 in order to find the checksum. If the remainder is 0, the message "checksum sum is 0" is displayed. If not then the remainder value is subtracted from 11 to compute the checksum. If the checksum computed is 10 then X is used as checksum digit.