Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(Financial: credit card number validation) in C++ Credit card numbers follow cer

ID: 3591987 • Letter: #

Question

(Financial: credit card number validation) in C++

Credit card numbers follow certain patterns. 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 are generated following this validity check, commonly known as the Luhn check or the Mod 10 check, which can be described as follows:

a. 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.

b. Now add all single-digit numbers from Step a.
c. Add all digits in the odd places from right to left in the card number.
d. Sum the results from Step b and Step c.

e. If the result from Step d 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

bool isValid(const string& cardNumber)

// Get the result from Step 2

int sumOfDoubleEvenPlace(const 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(const string& cardNumber)

// Return true if substr is the prefix for cardNumber

bool startsWith(const string& cardNumber, const string& substr)

Here are sample runs of the program:

Enter a credit card number without any space: 4388576018410707

4388576018410707 is valid

Explanation / Answer

#include <bits/stdc++.h>
using namespace std;

// Return true if substr is the prefix for cardNumber
bool startsWith(const string& cardNumber, const string& substr){
  
if(substr.size()>cardNumber.size())
return false;
  
for(int i=0;i<substr.size();i++){
if(cardNumber[i]!=substr[i])
return false;
}
return true;
}

// Return sum of odd-place digits in the card number (step c)
int sumOfOddPlace(const string& cardNumber){
int sum=0;
for(int i=cardNumber.size()-1;i>=0;i-=2){
int number = cardNumber[i]-'0';
sum+=number;
}
return sum;
}

// Return this number if it is a single digit, otherwise,
// return the sum of the two digits

int getDigit(int number){
  
//return number if it is single digit
if(number<10)
return number;
  
//add first digit
int sum = number/10;
  
//add second digi
sum+=number%10;
  
return sum;
}

// Get the result from Step b
int sumOfDoubleEvenPlace(const string& cardNumber){
int sum=0;
for(int i=cardNumber.size()-2;i>=0;i-=2){
int number = cardNumber[i]-'0';
sum+=getDigit(2*number);
}
return sum;
}

// Return true if the card number is valid
bool isValid(const string& cardNumber){
  
//sum of results from step c and step b (result of step d)
int sumOddEven = sumOfDoubleEvenPlace(cardNumber) + sumOfOddPlace(cardNumber);
  
//If the result from Step d is divisible by 10, the card number is valid
if(sumOddEven%10==0)
return true;
return false;
}


//main program
int main(){

//take card number as input from user
long cardNumber;
cin>>cardNumber;
  
//convert long to string
string cardString = to_string(cardNumber);
  
//check if card number is valid or not
if(isValid(cardString))
cout<<"Card number "<<cardString<<" is a valid number";
else
cout<<"Card number "<<cardString<<" is not a valid number";
  
return 0;
}