Please write your own source code and make sure that it compiles. C++ Programmin
ID: 3841312 • 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.
Explanation / Answer
#include<iostream>
using namespace std;
int getDigit(int number) {
int sum = 0;
while (number > 0) {
sum += number%10;
number /= 10;
}
return sum;
}
int sumOfDoubleEvenPlace(long long number) {
int sum = 0;
int tmp;
while (number > 9) {
number /= 10;
tmp = number % 10;
sum = sum +getDigit(2*tmp);
number /= 10;
}
return sum;
}
int sumOfOddPlace(long long number) {
int sum = 0;
while (number > 0) {
sum += number % 10;
number /= 100;
}
return sum;
}
bool prefixMatched(long long number, int digit) {
int tmp = 0;
while (number > 0) {
tmp = number % 10;
number /= 10;
}
return tmp == digit;
}
int getSize(long long number) {
int count = 0;
while (number > 0) {
count++;
number /= 10;
}
return count;
}
long getPrefix(long long number, int numDigits) {
int size = getSize(number);
if (size <= numDigits)
return number;
size = size - numDigits;
for (int i = 0; i<size; i++)
number /= 10;
return number;
}
bool isValid(long long number) {
return (sumOfDoubleEvenPlace(number) + sumOfOddPlace(number))%10 == 0;
}
int main() {
long long number;
cout << "Enter a credit card number: ";
cin >> number;
cout << number << " is ";
if (isValid(number))
cout << "valid";
else
cout << "invalid";
cout << endl;
}
Here you go champ. I hopey understand this. Let me know if you need explanation on this answer.