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

Please write your own source code and make sure that it compiles. C++ Programmin

ID: 3841446 • Letter: P

Question

Please write your own source code and make sure that it compiles. C++ Programming.CreditCardValidator.cpp. Include some comments on the source code.

Credit Card Validator
Credit card numbers follow certain patterns. A credit card number must have between 13 and 16 digits. In addition, cards have prefixes by type:
4 – Visa 5 – Mastercard 37 – American Express 6 – Discover
In 1954, Hans Luhn of IBM proposed an algorithm for validating credit card numbers. The algorithm is useful to determine whether a card 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. The Luhn check can be described as follows. 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 digits to get a single-digit number.

4 3 8 8 5 7 6 0 1 8 4 0 2 6 2 6

22=4

22=4

42=8

12=2

62=12 1+2=3

52=10 1+0=1

82=16 1+6=7

42=4

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. Add the results from the previous two steps.

37+38=75

5. If the result of the addition 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. Display whether the number is valid or invalid. Design your program to use the following functions:

// Return true if the card number is valid.

bool isValid(long long number)

// Get the result of Step 2.

int sumOfDoubleEvenPlace(long long number)

// 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 number.

int sumOfOddPlace(long long number)

// Return true if the digit is a prefix for this number.

bool prefixMatched(long long number, int digit)

// Return the number of digits in number

int getSize(long long number)

// Return the first numDigits digits from number. If the no. of digits

// in number is less than numDigits, return number.

long getPrefix(long long number, int numDigits)

Here are example runs of the program:

Enter a credit card number: 4388576018402626

4388576018402626 is invalid.

Enter a credit card number: 4388576018410707

4388576018410707 is valid.

Enter a credit card number: 48706286977

48706286977 is invalid.

Explanation / Answer

#include <iostream>
using namespace std;
bool isValid(long long number);
int sumOfDoubleEvenPlace(long long number);
int getDigit(int number);
int sumOfOddPlace(long long number);
bool prefixMatched(long long number, int digit);
int getSize(long long number);
int sumOfDouble(int n);
long getPrefix(long long number, int numDigits);

bool isValid(long long number){
int res;
res=sumOfDoubleEvenPlace(number)+sumOfOddPlace(number);
if(res%10==0){
return 1;
}
else{
return 0;
}
}

int getSize(long long number){
unsigned int number_of_digits = 0;

do {
++number_of_digits;
number /= 10;
} while (number);
return number_of_digits;
}
int sumOfDouble(int n){
if((n*2)<10){
return (n*2);
}
else{
return ((n*2)-9);
}
}
int sumOfDoubleEvenPlace(long long number){
int a[16],i,j,size,sum=0;
size=getSize(number);
for (i = (size-1); i >= 0; i--) {
a[i] = number % 10;
number /= 10;
}
for(j=(size-2);j>=0;j=j-2){
sum=sum + sumOfDouble(a[j]);
}
return sum;
}

int sumOfOddPlace(long long number){
int a[16],i,j,size,sum=0;
size=getSize(number);
for (i = (size-1); i >= 0; i--) {
a[i] = number % 10;
number /= 10;
}
for(j=(size-1);j>=0;j=j-2){
sum=sum + a[j];
}
return sum;
}

int main ()
{
int n;
bool result;
cout << "Enter the Card Number : ";
cin >> n;
result=isValid(n);
if(getSize(n)>=13){
if(result==1){
cout << "This is a Valid Card Number";
}
else{
cout << "This is an Invalid Card Number";
}
}
else{
cout << "This is an Invalid Card Number";
}
/*
cout << " ";
cout << getSize(4388576018410707);
cout << " ";
cout << sumOfDoubleEvenPlace(4388576018410707);
cout << " ";
cout << sumOfOddPlace(4388576018410707);
cout << " ";
cout << "The result is : " << result;
cout << " ";
*/
}