Luhn's Algorithm

Need help using it

Page 1 of 1

2 Replies - 8420 Views - Last Post: 04 August 2009 - 11:01 PM Rate Topic: -----

#1 LJ080805  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 29-March 09

Luhn's Algorithm

Posted 04 August 2009 - 10:16 PM

//Finds out if the credit card is valid or not
public boolean isValid() {
	//Finds out if credit card number is even or odd number of digits
	temp = cc_num.size() % 2;

	//Performs Luhn's Algorithm
	for(i = temp; i < cc_num.size(); i += 2) {
		if(cc_num.elementAt(i) * 2 > 9)
			temp = cc_num.elementAt(i) * 2 - 9;
		else
			temp = cc_num.elementAt(i) * 2;
		sum += temp;
	}
	//Returns true or false based on the statement below
	return sum % 10 == 0;
}



What's happening is that it is always returning false given the input by my instructors solution program.
A sample input would be 378282246310005
That is a valid card number but when my program performs the algorithm it doesn't work. I already having a working algorithm for Luhn's Algorithm but my instructor said it was redundant code and I should make it more concise.

Luhn's Alogithm should look like this:
"For a card with an even number of digits, double every odd numbered digit and subtract 9 if the product is greater than 9. Add up all the even digits as well as the doubled-odd digits, and the result must be a multiple of 10 or it's not a valid card. If the card has an odd number of digits, perform the same addition doubling the even numbered digits instead."
For example, "[Let's] apply the Luhn check to 4408 0012 3456 7890. (4*2)+4+(0*2)+8+(0*2)+0+(1*2)+2+(3*2)+4+(5*2-9)+6+(7*2-9)+8+(9*2-9)+0 = 8+4+0+8+0+0+2+2+6+4+1+6+5+8+9+0 = 63, which is not a multiple of 10. Therefore we conclude that the number 4408 0012 3456 7890 is an invalid credit card number."

Is This A Good Question/Topic? 0
  • +

Replies To: Luhn's Algorithm

#2 Guest_Neumann*


Reputation:

Re: Luhn's Algorithm

Posted 04 August 2009 - 10:33 PM

You're only adding the digits that you double. What about the other ones?

This post has been edited by Neumann: 04 August 2009 - 10:38 PM

Was This Post Helpful? 1

#3 LJ080805  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 29-March 09

Re: Luhn's Algorithm

Posted 04 August 2009 - 11:01 PM

Ohhh ok that makes total sense now, can't believe I overlooked that.
Thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1