Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am supposed to find if cc numbers are valid based on these rules:

Multiply every other digit by 2, starting with the number’s second-to-last digit, and then add those products' digits together.

Add the sum to the sum of the digits that weren’t multiplied by 2.

If the total’s last digit is 0 (or, put more formally, if the total modulo 10 is congruent to 0), the number is valid!

When I work-it-out by hand, I dont get valid for these numbers... but they are (from the solutions site). Can I see how its done correctly?

371449635398431

5105105105105100

4111111111111111

4012888888881881


Thanks

What I have tried:

I worked it out on paper. I tried my best to follow the rules exactly. I did get it right on other numbers, but not these.
Posted
Updated 26-Dec-16 20:41pm
v2
Comments
[no name] 26-Dec-16 22:11pm    
We aren't here to teach you programming or write code for you. Do what you have already been told and learn how to use the debugger and find out why your code is not behaving as you expect it to instead of reposting your not a question over and over.

1 solution

Let's perform the operation on the first input code:

371449635398431

every other digit (starting from second-to-last)
3 8 3 3 9 4 7
doubled and summed up
6 + 16 + 6 + 6 + 18 + 8 + 14 = 74
remaining digits (starting from the left), summed up
3 + 1 + 4 + 6 + 5 + 9 + 4 + 1 = 33

Total = 107


So, at least I can confirm that, according to the rules depicted, the first input code should be rejected.

Either we're right (and the 'solution site' is wrong) or we're wrong (bad computation) or the reported rules are miswritten (or misunderstood).


[update]
According to this site Credit Card Number Generator & Validator - FreeFormatter.com[^] such input is a valid credit card number. However the rules followed are a bit different, namely they follow the Luhn algorithm. Since the algorithm is well described in the same site, I suggest you to have a look at it.
[/update]
 
Share this answer
 
v2
Comments
Maciej Los 27-Dec-16 2:52am    
5ed!
CPallini 27-Dec-16 2:54am    
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900