In: Computer Science
Using Perl language: Write a program that reads a string from the standard input, and uses a Perl regular expression to test whether the string looks like a valid credit card number. The card numbers of four major companies follow the following formats:
Visa
13 or 16 digits, starting with 4.
MasterCard
16 digits, starting with 51 through 55.
Discover
16 digits, starting with 6011 or 65.
American Express
15 digits, starting with 34 or 37.
You should test your script with following numbers:
(1)4941 5968 0539 2447
(2) 371642190784801
(3) 5168441223630339
(4) 6011988461284820
Please include screenshots for your output.
program
OUTPUT 1
OUTPUT 2
OUTPUT 3
OUTPUT 4
PROGRAM IN DOCUMENT FORMAT
print "Enter card number please\n";
$card = <>; #variable to hold card number
chomp $card; #remove \n value from string variable
print "Entered card number is",$card;
$cardlen = length($card); #found length of card number
$card = $card*1; #convertion from string to integer
while($card>0)
{
$n=int($card%10);
$rev=int($rev*10)+$n; #while loop to find reverse of card
number
$card=int($card/10);
}
$temp2=$rev%100;
while($temp2>0)
{
$n=int($temp2%10);
$rev2=int($rev2*10)+$n; #while loop for extracting first 2 digits
from card number
$temp2=int($temp2/10);
}
$temp4=$rev%10000;
while($temp4>0)
{
$n=int($temp4%10);
$rev4=int($rev4*10)+$n; #while loop for extracting first 4 digits
from card number
$temp4=int($temp4/10);
}
if($cardlen==13) #if condition to check whether card number
length is 13
{
if($rev%10==4) #if condition to check whether the first digit of
card number is 4
{
print "\n You entered a valid Visa card number.\nThankyou\n";
}
else
{
print "\n Invalid card number.";
}
}
elsif($cardlen==16) #if condition to check whether card number
length is 16
{
if($rev%10==4) #if condition to check whether the first digit of
card number is 4
{
print "\n You entered a valid Visa card number.\nThankyou\n";
}
elsif($rev2>=51 && $rev2<=55) #if condition to check
whether the first 2 digit of card number is 51 through 55
{
print "\n You entered a valid Master card
number.\nThankyou\n";
}
elsif($rev4==6011 || $rev4==65)#if condition to check whether the
first 4 digit of card number is 6011 or it is equalto 65
{
print "\n You entered a valid Discover card
number.\nThankyou\n";
}
else
{
print "\n Invalid card number.";
}
}
elsif($cardlen==15) #if condition to check whether card number
length is 15
{
if($rev2==34 || $rev2==37) #if condition to check whether the first
digit of card number is 34 or 37
{
print "\n You entered a valid American express card
number.\nThankyou\n";
}
else
{
print "\n Invalid card number.";
}
}
else
{
print "\n Invalid card number.";
}
NOTE:
If this solution is helpful to you please thumbs up(like)
Thankyou