Luhn test of credit card numbers: Difference between revisions

Add CLU
m (→‎{{header|Picat}}: Added {{out}})
(Add CLU)
Line 2,122:
(doseq [n ["49927398716" "49927398717" "1234567812345678" "1234567812345670"]]
(println (luhn? n)))</lang>
 
=={{header|CLU}}==
<lang clu>luhn = proc (num: string) returns (bool) signals (bad_format)
total: int := 0
even: bool := true
for i: int in int$from_to_by(string$size(num), 1, -1) do
digit: int := int$parse(string$c2s(num[i])) resignal bad_format
even := ~even
if even then
digit := 2 * digit
if digit >= 10 then digit := digit//10 + 1 end
end
total := total + digit
end
return(total // 10 = 0)
end luhn
 
start_up = proc ()
po: stream := stream$primary_output()
tests: sequence[string] := sequence[string]$
["49927398716", "49927398717", "1234567812345678", "1234567812345670"]
for test: string in sequence[string]$elements(tests) do
stream$puts(po, test || ": ")
if luhn(test)
then stream$putl(po, "pass")
else stream$putl(po, "fail")
end
end
end start_up</lang>
{{out}}
<pre>49927398716: pass
49927398717: fail
1234567812345678: fail
1234567812345670: pass</pre>
 
=={{header|COBOL}}==
2,093

edits