C++ was employed in developing the Credit Card Validator program, which utilizes the Luhn algorithm to validate credit card numbers and determine their card type. The application, built using the C++ programming language, verifies the authenticity of a user's credit card number.
It employs the Luhn algorithm to ascertain the validity of a credit card number. Luhn's technique is a simple checksum algorithm that is applicable for authenticating various identifications like credit card numbers, IMEI numbers, and others, with its primary usage being in the validation of credit card numbers.
Overview
After inputting the credit card number, the system validates it to confirm if it belongs to Visa, MasterCard, American Express, or another card type. This validation process involves conducting fundamental checks and operations to confirm the card's legitimacy and brand. Users receive feedback on the validity of the entered credit card number, accompanied by a message specifying if the card is valid or invalid. In cases where the credit card number is valid, the system also displays the type of credit card associated with it.
There are patterns in credit card numbers.
Credit card number must contain 13 to 16 digits. It must start with:
- for Visa cards four
- for Master cards five
- 37 for cards from American Express
- 6 for cards from Discover
Luhn's algorithm is applicable for resolving the issue at hand.
Luhn Algorithm
Luhn's algorithm, also known as the modulus 10 or mod 10 algorithm, serves as a simple validation method used to authenticate various identification numbers like Canadian social insurance numbers, IMEI numbers, and credit card numbers. The LUHN formula was developed by a group of mathematicians in the late 1960s and was swiftly embraced by credit card companies. This algorithm is publicly available, making it accessible to anyone. It is commonly employed to differentiate between valid numbers and those that may have been input incorrectly on numerous credit cards and government-issued identification numbers. Its primary purpose is to safeguard against unintentional errors rather than intentional attacks.
The Luhn algorithm, also referred to as the Mod 10 algorithm, can be described as follows (using the card number 4388576018402626 for illustration):
Step 1: Begin by doubling every second digit from the right. If the result is a double-digit number, add the two digits together to get a single-digit number.
Combine all the numbers that were single digits in Step 1 and add them together in this current step.
4 + 4 + 8 + 2 + 3 + 1 + 7 + 8 = 37
Step 3: Sum up all digits located in the odd positions of the card number, starting from the right and moving towards the left.
6 + 6 + 0 + 8 + 0 + 7 + 8 + 3 = 38
Add the results from Steps 2 and 3 together. The sum of 37 and 38 is 75.
Step 5: A card number is considered valid if the result from Step 4 can be evenly divided by 10; otherwise, it is deemed invalid.
Program Break Down
#include < iostream >
#include < bits/stdc++.h >
#include < math >
We are incorporating all the necessary files for credit card validation in C++.
int getDigit( int number )
{
if ( number < 9 )
return number;
return number / 10 + number % 10;
}
The getDigit function mentioned above will provide the sum of two digits if present, or return the number itself if it is a single digit.
int getSize( long d )
{
string num = to_string( d );
return num.length( );
}
The function described earlier will provide the count of digits in the variable d.
long getPrefix( long number , int k )
{
if (getSize( number ) > k)
{
string num = to_string( number );
return stol( num.substr( 0 , k ));
}
return number;
}
The previously discussed function, getPrefix, retrieves the initial k digits from a given number. In cases where the number of digits in the input number is fewer than k, the function will simply return the original number.
bool prefixMatched( long number , int d )
{
return getPrefix( number , getSize ( d )) == d;
}
The function prefixMatched will yield a true value if the digit d serves as a prefix for the number. Obtain the outcome as directed in Step 2.
int sumOfDoubleEvenPlace( long int number )
{
int sum = 0;
string num = to_string( number );
for (int i = getSize( number ) - 2; i > = 0; i - = 2)
sum + = getDigit( int( num [ i ] - ' 0 ' ) * 2);
return sum;
}
The previously discussed function will provide the outcome obtained in step 2.
int sumOfOddPlace( long number )
{
int sum = 0;
string num = to_string( number );
for (int i = getSize( number ) - 1; i > = 0; i - = 2)
sum + = num[ i ] - ' 0 ';
return sum;
}
The function mentioned earlier, sumOfOddPlace, calculates and returns the total of digits in odd positions within a given number.
bool isValid( long int number )
{
return (getSize( number ) >= 13 & &getSize( number ) <= 16) && ( prefixMatched( number , 4) || prefixMatched(number , 5) || prefixMatched( number , 37) || prefixMatched( number, 6)) && ((sumOfDoubleEvenPlace ( number ) + sumOfOddPlace( number )) % 10 = = 0);
}
The aforementioned function, named isValid, is of boolean type and will yield a true value if the user-inputted card number is considered valid.
int main( )
{
long int number = 5196081888500645L;
cout << number << " is " << (isValid( number ) ? " valid " : " invalid ");
return 0;
}
The driver code will appear similar to this, featuring a lengthy integer representing the credit card number. Subsequently, a ternary operator is employed to validate the return of the function isValid. If the function returns true, the output will display as valid; otherwise, it will display as invalid.
Program for Credit Card Validator in C++
// C++ program to check if a given credit
// card is valid or not.
#include <iostream>
using namespace std;
// Return this number if it is a single digit, otherwise,
// return the sum of the two digits
int getDigit(int number)
{
if (number < 9)
return number;
return number / 10 + number % 10;
}
// Return the number of digits in d
int getSize(long d)
{
string num = to_string(d);
return num.length();
}
// Return the first k number of digits from
// number. If the number of digits in number
// is less than k, return number.
long getPrefix(long number, int k)
{
if (getSize(number) > k)
{
string num = to_string(number);
return stol(num.substr(0, k));
}
return number;
}
// Return true if the digit d is a prefix for number
bool prefixMatched(long number, int d)
{
return getPrefix(number, getSize(d)) == d;
}
// Get the result from Step 2
int sumOfDoubleEvenPlace(long int number)
{
int sum = 0;
string num = to_string(number);
for (int i = getSize(number) - 2; i >= 0; i -= 2)
sum += getDigit(int(num[i] - '0') * 2);
return sum;
}
// Return sum of odd-place digits in number
int sumOfOddPlace(long number)
{
int sum = 0;
string num = to_string(number);
for (int i = getSize(number) - 1; i >= 0; i -= 2)
sum += num[i] - '0';
return sum;
}
// Return true if the card number is valid
bool isValid(long int number)
{
return (getSize(number) >= 13 &&
getSize(number) <= 16) &&
(prefixMatched(number, 4) ||
prefixMatched(number, 5) ||
prefixMatched(number, 37) ||
prefixMatched(number, 6)) &&
((sumOfDoubleEvenPlace(number) +
sumOfOddPlace(number)) %
10 ==
0);
}
// Driver Code
int main()
{
long int number = 51960818885000645L;
cout << number << " is " << (isValid(number) ? "valid" : "invalid");
return 0;
}
Output:
5116021318510645L is valid.
............................
Process executed in 1.22 seconds
Press any key to continue.
The time complexity is O(n), with n representing the size of the given string.
Auxiliary Space: O(1); remains constant as it requires no extra space.