(.Financial: credit card number validation) Credit card numbers follow certain p
ID: 3531082 • Letter: #
Question
(.Financial: credit card number validation) Credit card numbers follow certain pal- terns. A credit card number must have between 13 and 16 digits. It must start with: 4 for Visa cards 5 for Master cards 37 for American Express cards 6 for Discover cards In 1954. Hans Luhn of IBM proposed an algorithm for validating credit card numbers. The algorithm is useful to determine whether a card number is entered correctly or whether a credit card is scanned correctly by a scanner. Credit card numbers arc generated following this validity check, commonly known as the Luhn check or the Mod 10 check, which can be described as follows (for illustration. consider the card number 4388576018402626): 1. Double every second digit from right to left. If doubling of a digit results in a two-digit number, add up the two digits to get a single-digit number. Now add all single-digit numbers from Step 1. 4 + 4 + 8 + 2 + 3+1+7 + 8 = 37 Add all digits in the odd places from right to left in the card number. 6+6 + 0 + 8 + 0 + 7 + 8 + 3 = 38 Sum the results from Step 2 and Step 3. 37 + 38 = 75 If the result from Step 4 is divisible by 10. the card number is valid: otherwise, it is invalid. For example, the number 4388576018402626 is invalid, but the number 4388576018410707 is valid. Write a program that prompts the user to enter a credit card number as a long integer. Display whether the number is valid or invalid. Design your program to use the following methods: Return true if the card number is valid public static boolean isValid(long number) Get the result from Step 2 public static int sumOfDoubleEvenPlace(long number) Return this number if it is a single digit, otherwise, return the sum of the two digits public static int getDigit(int nurber) Return sum of odd-place digits in number public static int sum0f0ddPlace(long number) Return true if the digit d is a prefix for number public static boolean prefixMatchedflong nurber, int d) Return the number of digits in d public static int getSize(long d) Return the first k number of digits from number. If the number of digits in number is less than k, return number. public static long getPrefix(long number, int k) Here arc sample runs of the program:Explanation / Answer
#include<sstream>
#include<string>
using namespace std;
bool creditCardType(string cardNumber)
{
return (cardNumber.compare(0, 1, "4") == 0) || // TYPE VISA.
(cardNumber.compare(0, 1, "5") == 0) || // TYPE MASTER.
(cardNumber.compare(0, 2, "37") == 0) || // TYPE AMERICAN EXPRESS
(cardNumber.compare(0, 1, "6") == 0); // TYPE DISCOVER.
}
int getDigit(int number)
{
int result =0;
while(number)
{
result = result + (number%10);
number = number/10;
}
return result;
}
// to calculate sum of numbers at even places.
int sumOfDoubleEvenPlaces(const string& cardNumber)
{
int sum_even_places = 0;
for (int i = cardNumber.length()-2; i>=0; i-=2)
{
sum_even_places = sum_even_places + getDigit(2*(cardNumber[i] - '0'));
}
return sum_even_places;
}
int sumOfOddPlace(const string& cardNumber)
{
int sum_odd_places = 0;
for (int i = cardNumber.length()-1; i>=0; i-=2)
{
sum_odd_places = sum_odd_places +((cardNumber[i] - '0'));
}
return sum_odd_places;
}
// function to check card is valid or not.
bool isValid(const string& cardNumber)
{
return ((sumOfDoubleEvenPlaces(cardNumber)+sumOfOddPlace(cardNumber))%10==0);
}
//Write a program that prompts the user to enter a credit card number as a long integer.
int main()
{
long int cardnNum;
int choice;
cout << "Enter your credit card number ";
cin >>cardnNum;
cout << endl;
stringstream ss;
ss << cardnNum;
string cardnumber = ss.str();
// You program will begin by asking the user what type of execution:
// 1= use asset logic on length of credit card number
// 2= print error message and continue on to next credit card number.
//Display the credit card number, length of credit card number and whether the number is valid or invalid.
cout <<"Enter 1 to use asset logic on length of credit card number" << endl;
cout <<"Enter 2 to use creditCardType logic on credit card number " << endl;
cin >> choice;
cout << endl;
if(choice == 1)
{
cout << cardnumber << " has Type ";
if(cardnumber[0] == '4') cout <<" VISA";
if(cardnumber[0] == '5') cout <<" MASTER";
if(cardnumber[0] == '6') cout <<" DISCOVER";
if(cardnumber[0] == '3') cout <<" AMERICAN EXPRESS";
cout << endl;
if((cardnumber.length()>=13 && cardnumber.length()<=16))
cout << cardnumber << " has length " <<cardnumber.length() <<" Is Valid card " << endl;
else
cout << cardnumber << " has length " <<cardnumber.length() <<" Is InValid card " << endl;
}
else if(choice == 2)
{
if(creditCardType(cardnumber) && isValid(cardnumber))
{
cout << cardnumber << " has Type ";
if(cardnumber[0] == '4') cout <<" VISA";
if(cardnumber[0] == '5') cout <<" MASTER";
if(cardnumber[0] == '6') cout <<" DISCOVER";
if(cardnumber[0] == '3') cout <<" AMERICAN EXPRESS";
cout <<" Is Valid card " << endl;
}
cout << cardnumber << " has Invalid Type.so "<< cardnumber<<" Is InValid card " << endl;
}
system("pause");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.