Luhn test of credit card numbers: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
No edit summary |
||
Line 3,135: | Line 3,135: | ||
1234567812345670 is valid |
1234567812345670 is valid |
||
</pre> |
</pre> |
||
=={{header|FutureBasic}}== |
|||
<syntaxhighlight lang="futurebasic"> |
|||
include "NSLog.incl" |
|||
local fn LuhnCheck( cardStr as CFStringRef ) as BOOL |
|||
'~'1 |
|||
NSInteger i, j, count, s1 = 0, s2 = 0 |
|||
BOOL result = NO |
|||
// Build array of individual numbers in credit card string |
|||
NSUInteger strLength = len(cardStr) |
|||
CFMUtableArrayRef mutArr = fn MutableArrayWithCapacity(strLength) |
|||
for i = 0 to strLength - 1 |
|||
CFStringRef tempStr = fn StringWithFormat( @"%C", fn StringCharacterAtIndex( cardStr, i ) ) |
|||
MutableArrayInsertObjectAtIndex( mutArr, tempStr, i ) |
|||
next |
|||
// Reverse the number array |
|||
CFArrayRef reversedArray = fn EnumeratorAllObjects( fn ArrayReverseObjectEnumerator( mutArr ) ) |
|||
// Get number of array elements |
|||
count = len(reversedArray) |
|||
// Handle odd numbers |
|||
for i = 0 to count - 1 step 2 |
|||
s1 = s1 + fn StringIntegerValue( reversedArray[i] ) |
|||
next |
|||
// Hnadle even numbers |
|||
for i = 1 to count - 1 step 2 |
|||
j = fn StringIntegerValue( reversedArray[i] ) |
|||
j = j * 2 |
|||
if j > 9 then j = j mod 10 + 1 |
|||
s2 = s2 + j |
|||
next |
|||
if (s1 + s2) mod 10 = 0 then result = YES else result = NO |
|||
end fn = result |
|||
NSLogClear |
|||
if fn LuhnCheck( @"49927398716" ) == YES then NSLog (@"%@ is valid.", @"49927398716" ) else NSLog (@"%@ is not valid.", @"49927398716" ) |
|||
if fn LuhnCheck( @"49927398717" ) == YES then NSLog (@"%@ is valid.", @"49927398717" ) else NSLog (@"%@ is not valid.", @"49927398717" ) |
|||
if fn LuhnCheck( @"1234567812345678" ) == YES then NSLog (@"%@ is valid.", @"1234567812345678" ) else NSLog (@"%@ is not valid.", @"1234567812345678" ) |
|||
if fn LuhnCheck( @"1234567812345670" ) == YES then NSLog (@"%@ is valid.", @"1234567812345670" ) else NSLog (@"%@ is not valid.", @"1234567812345670" ) |
|||
HandleEvents |
|||
</syntaxhighlight> |
|||
{{output}} |
|||
<pre> |
|||
49927398716 is valid. |
|||
49927398717 is not valid. |
|||
1234567812345678 is not valid. |
|||
1234567812345670 is valid. |
|||
</pre> |
|||
=={{header|Gambas}}== |
=={{header|Gambas}}== |