Credit card companies are responsible for a high volume of highly sensitive global network traffic per minute with no margin for error. These companies need to ensure they are not wasting resources processing unnecessary requests. When a credit card is run, the processor has to look up the account to ensure it exists, then query the balance to ensure the amount requested is available. While an individual transaction is cheap and small, the scales involved are enormous.
Credit Card Payment Method
Multiply every other digit by 2, starting with the number’s second-to-last digit, and then add those products’ digits together.
Add the sum to the sum of the digits that weren’t multiplied by 2.
If the total’s last digit is 0 then the number is valid!
Take for example the following American Express number, 378734493671000. Starting from the second-to-last-digit, multiply the last number by 2.
72 + 72 + 42 + 92 + 62 + 12 + 0*2
14 + 14 + 8 + 18 +12 + 2 + 0
Adding the product digits:
1 + 4 + 1 + 4 + 8 + 1 + 8 +1 + 2 + 2 + 0 = 32
Finally add the digits that were not multiplied to the sum
32 + 3 + 8 + 3 + 4 + 3 + 7 + 0 + 0 = 60
The checksum 60 ends with the number 0, therefore it a syntactically sound credit card number
Aside from the checksum, credit card number numbers also identify the type of credit card company. Visa cards start with the number 4. MasterCards start with the number 51, 52, 53, 54, or 55. American Express starts with the number 34 or 37.
The solution can be broken down into two parts:
Let’s take a look at the syntax for C and walkthrough the code.
To check if the card number is valid, there is a preliminary check that we can check for before calculating the checksum. We know that a credit card number can only be either be 13, 15, or 16. We can do that with a simple while or for loop.
The user’s card number is stored in the variable card number and on each iteration of the length of the number the last digit will be removed and counted. The count of the length of the number will then be checked with if it is either 13, 15, or 16 digits.
After passing the first test, the next step would be to see if the checksum is valid. Again we, will take a look at the syntax in C.
In this example, the array number is declared and the card numbers are enumerated through and each digit is saved in the array number. The digits that are stored in the array using this method is reversed from the original card number because of the operation of removing the last digit first and storing it in the first index.
Take for example the credit card number is 4012 8888 8888 1881. Using the modulo and divide by 10 method to store the array the resulting array would be [1,8,8,1,8,8,8,8,8,8,8,8,2,1,0,4].
The checksum for C was cleaner to implement for this reason that there were less type conversions needed to manipulate the data. The array used simple for loops and conditional statements to validate the number.
In the C code above, a new array was created to clone the number array and starting from the second-to-last value the value was multiplied by 2.
The bulk of the validation occurs in this nested if statement. First the length of the card number is determined. Then the arrays digits are added up and checked if the checksum is valid in this one line.
sumdigit = (number[i] % 10) + (number[i]/10 % 10);
The type of card is validated by checking the first and second index of the array. In this example, Visa cards start with the number 4.
cardarray == 4 && accumulator % 10 == 0
The array that has been multiplied by 2 is then summed and added into the array that has not being multiplied using the reduce method. If the checksum passes, then the first two digits of the card array is sliced to check what type of card. The implementation of the card type check is similar to the C syntax. By using a conditional statement the digits of the card array is then evaluated for each type.
This post is more of a self reflection on the differences in programming in a lower level language and a higher level language. In attempting to create a credit card checker, I have found that the lower level language syntax to be more concise in getting to the solution, whereas using the higher level language requires data type conversions to use the higher order functions.
I’m still learning more about code every day. I would love to hear from you, if you have any tips or suggestions.
Thank you !
C++ is general purpose, compiled, object-oriented programming language and its concepts served as the basis for several other languages such as Java, Python, Ruby, Perl etc.
In this Video We are going to see how to use Loops in C++. We will see How to use For, While, and Do While Loops in C++.
In this article, we'll take a look at using the isdigit() function in C/C++. This is a very simple way to check if any value is a digit or not. Let's look
Welcome to this course on C++ Tutorial for Beginners. In this video we will see How to use C++ Structures.
In this article, we’ll take a look at using fread() in C/C++.