In the 1950's, Hans Peter Luhn invented a method for checking the validity of ID numbers. This method (known as the Luhn Algorithm or the Luhn Formula) is still used today for a number of different purposes, including all major credit card numbers and Social Insurance Numbers.
Here's how the Luhn Algorithm works when checking for a valid ID number:
- Starting from the right, double every second digit, add up the digits of the result, and total up all the resulting numbers.
- Add to this total the sum of all the remaining digits.
- If the result is divisible by , the ID number is valid.
Example 1: Validate 42395
Step 1
Step 2
Step 3
is not divisible by .
Not valid.
Example 2: Validate 35436
Step 1
Step 2
Step 3
is divisible by .
Valid.
Explanation
The last digit of every ID number is the "check digit" and the rest of it is the base number. So in the first example above, 4239
is the base number and 5
is the check digit. When generating ID numbers, you first generate the base number without the final digit, then you figure out what the check digit has to be to make the whole ID number valid.
Specification
The input will contain test cases. Each test case consists of a batch of base numbers ( to digits each) on one line, each separated by a single space character. Your job is to compute the check digit for each base number in the batch and then output the result as a single -digit number.
Sample Input
389796 4565280784 8451692334 46 465949539
97699 7392253 54011409 8073542288 303142477
334 349839 12593962 02497993 9468
53173 2901524 2493367526 39094 83530
08080532 5023002 57849 9853641952 027179
Sample Output
48336
36757
31920
15686
88201
Educational Computing Organization of Ontario - statements, test data and other materials can be found at ecoocs.org
Comments
java.util.InputMismatchException Can the input not be taken in as long?
Since each number can be up to 100 digits, and a long can hold around 19 digits, you will need to find a different way to store the input.
So are all these numbers presented here inputs? If so, where are the outputs? Also, if we are given, say a 6 digit number, are we supposed to find the check digit and then add it to make it into a 7 digit number? Because the last sentence says "output the result as a single 5-digit number" which doesn't make sense.
This comment is hidden due to too much negative feedback. Show it anyway.