Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
Luhn Algorithm Computer Code---checking to see if credit card #'s are valid..?
Array d of ints contains the digits of a credit card number, including the check digit in last place. Complete the following code to check (using the Luhn algorithm) whether the credit card number is valid, storing true in valid if and only if it is.
The for-loop in the following code deals with the digits in left-to-right order. Notice, however, that the digit with index i occupies right-to-left position d.length - i. For example, the last digit (which has index d.length - 1) is in right-to-left position
d.length - (d.length - 1) = 1,
and the first digit (which has index 0) is in right-to-left position
d.length - 0 = d.length.
// Enter values to test here
int[] d = { };
boolean valid;
int sum = 0;
int dd;
for ( int i = 0; i < d.length; i++ )
{
// Is right-to-left position even?
if ( /* Your code here */ )
{
// If it is, set dd to twice the digit
dd = /* Your code here */ ;
// Add the sum of dd's digits to sum
sum += /* Your code here */ ;
}
else
{
// If right-to-left position is odd,
// add the digit to sum
sum += /* Your code here */ ;
}
}
valid = /* Your code here */ ;
1 Answer
- 1 decade agoFavorite Answer
Please add YOUR code where it says /* your code here */. I would assume that the instructor that gave you this homework assignment would want you to do your own work.
After you add YOUR code, and are having problems, then come back.