Javatpoint Logo
Javatpoint Logo

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.


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

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

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

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

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.

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

The above mentioned function will return the result which was got from the step 2

The function above i.e. sumOfOddPlace will return the sum of odd place digits in number.

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.

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++


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.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA