Credit Card Validator In C++

C++ was used to create the Credit Card Validator application. It uses the Luhn algorithm to verify the credit card number and identify the type of credit card. The C++ programming language was used to create a Credit Card Validator application that verifies the validity of a user's credit card number.

It uses the Luhn algorithm to determine whether a credit card number is legitimate or not. Luhn's method is a straightforward checksum formula that can be used to verify a variety of identifications, including credit card numbers, IMEI numbers, and more, although it is most commonly used to verify credit card numbers.

Overview

After entering the credit card number, the system verifies its validity and determines whether it is a Visa, MasterCard, American Express or another form of card. It verifies the authenticity of the card and the type of credit card by performing some basic operations and validations. Credit card numbers entered by the user are generated along with a message indicating whether the card is valid or invalid. If the credit card number entered is valid, the credit card type will also be printed.

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 can be used to solve the problem.

Luhn Algorithm

Luhn's method, commonly referred to as the modulus 10 or mod 10 algorithm, is a straightforward checksum technique used to verify a large number of identification numbers, including Canadian social security numbers, IMEI numbers, and credit card numbers. A team of mathematicians invented the LUHN formula in the late 1960s. Credit card companies then quickly adopted it. Anyone can use the algorithm because it is freely available in the public domain. This technique is a common way to distinguish between legitimate numbers and numbers that have been entered incorrectly or otherwise on most credit cards and many government identification numbers. It was created to defend against inadvertent errors rather than deliberate attacks.

The Luhn check, often known as the Mod 10 check, may be explained as follows (for reference, use the card number 4388576018402626:

Step 1: From right to left, double every other digit. To produce a single-digit number while doubling a digit, put the two digits together (for example, 12:1+2, 18:1+8).

Step 2: Add all of the single-digit numbers from Step 1 in Step 2 now.

4 + 4 + 8 + 2 + 3 + 1 + 7 + 8 = 37

Step 3: Add all of the card number's digits in the odd spots, going from right to left.

6 + 6 + 0 + 8 + 0 + 7 + 8 + 3 = 38

Step 4: Add the outcomes of Steps 2 and 3. 37 + 38 = 75

Step 5: The card number is legitimate if the answer to Step 4 is divisible by 10; else, it is invalid.

Program Break Down

Example

#include < iostream >
#include < bits/stdc++.h >
#include < math >

We are including all the required files for credit card validator in C++.

Example

int getDigit( int number )
{
    if ( number < 9 )
        return number;
    return number / 10 + number % 10;
}

The above function i.e. getDigit will return the sum of the two digits otherwise return the number if it is a single digit.

Example

int getSize( long d )
{
    string num = to_string( d );
    return num.length( );
}

The function mentioned above will return the number of digits in d

Example

long getPrefix( long number , int k )
{
    if (getSize( number ) > k)
    {
        string num = to_string( number );
        return stol( num.substr( 0 , k ));
    }
    return number;
}

The above mentioned function i.e. getPrefix will return the first k number of digits from number. If the number of digits in number is less than k, then it will return the number.

Example

bool prefixMatched( long number , int d )
{
    return getPrefix( number , getSize ( d )) == d;
}

The function prefixMatched will return true if the digit d is a prefix for number. // Get the result from Step 2

Example

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 above mentioned function will return the result which was got from the step 2

Example

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 above i.e. sumOfOddPlace will return the sum of odd place digits in number.

Example

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 above mentioned function i.e. isValid is of bool type and will return true if the card number entered by the user is valid.

Example

int main( )
{
    long int number = 5196081888500645L;
    cout << number << " is " << (isValid( number ) ? " valid " : " invalid ");
    return 0;
}

The driver code will something look like this, containing a long integer number that will be the credit card number. Then using ternary operator we are checking if the function isValid return true then it will print valid otherwise it will print invalid.

Program for Credit Card Validator in C++

Example

// 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:

Output

5116021318510645L is valid.
............................
Process executed in 1.22 seconds
Press any key to continue.

Time Complexity is O(n), where n is the length of the supplied string.

Auxiliary Space: O(1); it is a constant since no additional space is needed.

Input Required

This code uses input(). Please provide values below: