The last digit of a credit card number is the check digit, which protects against transcription errors such as an error in a single digit or switching two digits. The following algorithm is used to verify actual credit card numbers, but for simplicity we will describe it for numbers with 8 digits instead of 16:
1. Starting from the rightmost digit, form the sum of every other digit. For example, if the credit card number is 43589795, then you form the sum 5 + 7 + 8 + 3 = 23.
2. Double each of the digits that were not included in the preceding step. Add all digits of the resulting numbers. For example, with the number given above, doubling the digits, starting with the next-to-last one, yields 18 18 10 8. Adding all digits in these values yields 1 + 8 + 1 + 8 + 1 + 0 + 8 = 27.
3. Add the sums of the two preceding steps. If the last digit of the result is 0, the number is valid. In our case, 23 + 27 = 50, so the number is valid.
Write a program (CreditCard.java) that implements this algorithm. The user should supply an 8-digit number, and the program should print out whether the number is valid or not.
Attachments
No attachments uploaded
Tags
Clarifications
/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
var disqus_shortname = 'cramshark'; // required: replace example with your forum shortname
var disqus_identifier = '/production/142';
/* * * DON'T EDIT BELOW THIS LINE * * */
(function () {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
Please enable JavaScript to view the comments powered by Disqus.

Attached is the source for...
Attachments
No screenshots uploaded

CreditCard.java (2 K)
Preview

Screenshots
validateNumber(number);
}
/**
* Validates credit card number
* @param number Credit card number
*/
private static void validateNumber(long number){
//Get total of even and odd digits
long total = evenSum(number) + oddSum(number);
//Check if total is divisible by 10