# AnalyticBridge

A Data Science Central Community

This code determines the type of card (Visa / Discover / MasterCard) and verifies whether the credit card number is a valid number. #!/usr/perl/bin

#Simple program to check validity of 16 digit credit card numbers

use strict;
use warnings;

sub checkNumber {
my \$in = shift;
my @cardNum = @{\$in};
my \$i = \$#cardNum - 1;

while (\$i >= 0) {
\$cardNum[\$i] = \$cardNum[\$i] * 2;
if (\$cardNum[\$i] >= 10) { \$cardNum[\$i] -= 9; }
\$i -= 2;
}

my \$total = 0;

\$total += \$_ foreach (@cardNum);
if (\$total % 10 == 0) {
return 1;
} else {
return 0;
}
}

sub cardInfo {
my \$cardNum = shift;
my \$issuer;
if (\$cardNum =~ /^4/) { \$issuer = "VISA" }
if (\$cardNum =~ /^5/) { \$issuer = "MasterCard" }
if (\$cardNum =~ /^6/) { \$issuer = "Discover" }
print "\n Card Number: \$cardNum\n";
print " Issuer: ", \$issuer,"\n\n";
}

print "Enter Number: ";
chomp(my \$cardNum = );

if (\$cardNum =~ /^[0-9]+\$/) {
my @number = split(//,\$cardNum);
if (\$#number == 15) {
if (checkNumber(\@number)) {
cardInfo(\$cardNum);
} else {
print "\nInvalid Card Number\n";
}
} else {
print "\n16 digit cards only for now.\n";
}
} else {
print "\nInvalid Input.\n";
}

Views: 3756