Please write your own source code and make sure that it compiles. C++ Programmin
ID: 3841425 • 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
The code is given as:
#include<iostream>
using namespace std;
int getSize(long long number)
{
int d=0;
while(number>=1)
{
d++;
number/=10;
}
return d;
}
long getPrefix(long long number, int numDigits)
{
if(number<numDigits)
return numDigits;
while(number>=numDigits*10)
number/=10;
return number;
}
bool prefixMatched(long long number, int digit)
{
int d=1;
if(digit>10)//checking for number of digits
d=2;
if(getPrefix(number,d)==digit)
return true;
return false;
}
int getDigit(int number)
{
if(number<10)
return number;
return (number/10+number%10);
}
int sumOfDoubleEvenPlace(long long number)
{
int s=0,d=1;
while(number>=1)
{
if(d%2==0)
s+=getDigit(2*(number%10));
number/=10;
d++;
}
return s;
}
int sumOfOddPlace(long long number)
{
int s=0,d=1;
while(number>=1)
{
if(d%2!=0)
s+=getDigit(number%10);
number/=10;
d++;
}
return s;
}
bool isValid(long long number)
{
bool x=true;
if(getSize(number)<13 || getSize(number)>16)//checking for validity of size
x=false;
else if(!prefixMatched(number,4) && !prefixMatched(number,6) && !prefixMatched(number,37) && !prefixMatched(number,5))//checking for prefix validity
x=false;
else if((sumOfDoubleEvenPlace(number)+sumOfOddPlace(number))%10!=0)
x=false;
else
x=true;
return x;
}
main()
{
long long n;
cout<<"Enter a credit card number: ";
cin>>n;
if(isValid(n))
cout<<n<<" is valid";
else
cout<<n<<" is invalid";
}