create a c++ program for the situation , Credit card numbers follow certain patt
ID: 3849163 • Letter: C
Question
create a c++ program for the situation ,
Credit card numbers follow certain patterns. A credit card number must have between 13 and 16 digits. The number must start with the following:
• 4 for Visa cards • 5 for MasterCard 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 is scanned correctly by a scanner. Almost all credit card numbers are generated following this validity check, commonly known as the Luhn check or the Mod 10 check. It 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 the two digits to get a single digit number. 4388576018402626: 2*2 =4, 2*2=4, 4*2=8, 1*2=2, 6*2=12 (1+2 =3), 5*2=10 (1+0=1) 8*2= 16(1+6=7), 4*2=8 2.
Now add all single -digit numbers from step 1. 4+4+8+2+3+1+7+8=37
3. Add all digits in the odd places from right to left in the card number. 6+6+0+8+0+7+8+3=38
4. Sum the results from step 2 and step 3. 37+38=75 5.
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 string. Display whether the number is valid. Design your program to use the following functions:
// Return true if the card number is
valid bool isValid (string cardNumber )
// Get the result from step 2
int sumOfDoubleEvenPlace (string cardNumber )
// Return this number if it is a single digit, otherwise, return the sum of the two //digits
int getDigit (int number)
// Return sum of odd-place digits in the card number
int sumOfOddPlace(string cardNumber )
// Return true if substr is the prefix for card number
bool startsWith (string cardNumber , string substr)
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.