Skip to content

Instantly share code, notes, and snippets.

@tuankiet65
Created January 8, 2013 11:37
Show Gist options
  • Save tuankiet65/4483132 to your computer and use it in GitHub Desktop.
Save tuankiet65/4483132 to your computer and use it in GitHub Desktop.
Check if a credit card number is valid using Luhn algorithm
{This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.}
program mod10;
uses crt;
var creditnum: int64;
num: array [1..100] of longint;
i, chks, i2: longint;
stri: string;
tmp, sum: int64;
begin
clrscr;
write('Enter credit card number: ');
readln(creditnum);
chks:=creditnum mod 10;
creditnum:=creditnum div 10;
str(creditnum, stri);
for i:=length(stri) downto 1 do begin
if i2 mod 2=0 then begin
num[i]:=creditnum mod 10;
num[i]:=num[i]*2;
end else
num[i]:=creditnum mod 10;
i2:=i2+1;
creditnum:=creditnum div 10;
end;
for i:=1 to length(stri) do begin
if num[i] div 10<>0 then begin
tmp:=num[i] mod 10;
num[i]:=num[i] div 10;
num[i]:=num[i]+tmp;
tmp:=0;
end;
end;
for i:=1 to length(stri) do sum:=sum+num[i];
sum:=sum*9;
textcolor(yellow);
if sum mod 10=chks then
writeln('The credit card number (', stri, ') is a valid credit card number')
else
writeln('The credit card number (', stri, ') is NOT a valid credit card number');
readln;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment