I am working on a Luhn Algorithm problem and am having trouble getting the sumDoubleSecondDigits() code to work properly. The credit card number that I am passing in is 4388576018402626. The intSum returned should = 37. I am able to get the odd number method to work, just not this one. Here are the directions that go along with the method:
1. Double every second digit from right to left. If doubling of a digit results in a two-digit number, add up the two digits to get a single-digit number.
2. Add up all the single digit numbers from step 1
3. Add up all digits in odd places from right to left in the card number.
4. Sum the results from Step 2 and Step 3
5. Divide the result from Step 4 by 10 and if there is no remainder than the number is valid. If there is a remainder the number is invalid.
Any help would be greatly appreciated!
//method to reduce numbers to a single digit
private int reduceNumber(int iNumberIn)
{
int intDigit = 0;
while(iNumberIn > 9)
{
// Get the right most digit:
intDigit += iNumberIn % 10;
// Remove the rightmost digit you just extracted:
iNumberIn /= 10;
} // end while()
return intDigit;
}//end reduceNumber method
//method to sum up double every second digit from right to left
private int sumDoubleSecondDigits(long lNumIn)
{
int intTempNum;
int intSum = 0;
while(lNumIn > 0)
{
intTempNum = 0;
// Remove the rightmost digit:
lNumIn /= 10;
// Get the right most digit:
intTempNum = (int)lNumIn % 10;
//Multiply the digit by 2
intTempNum *= 2;
reduceNumber(intTempNum);
// Remove the rightmost digit:
lNumIn /= 10;
intSum += intTempNum;
} // end while()
return intSum;
}

New Topic/Question
Reply




MultiQuote







|