Question: Luhn's Algorithm Problem Statement We need check credit card numbers, and find out if they are valid credit card number or not. Credit card numbers
Luhn's Algorithm Problem Statement We need check credit card numbers, and find out if they are valid credit card number or not. Credit card numbers use an algorithm called "Luhn's algorithm". Luhn's algorithm uses something called a check digit which is the last digit in the credit card number. The credit card number must pass the following test: Find the check digit the rightmost digit Using the example number the check digit is From the check digit, move left and, double the value of every second digit. Using the example number every second number would be: If we double one of these numbers and the result is greater than eg then add the digits of the product eg: : Note: The same result can be found by subtracting from the product eg: : Take the sum of all the digits from step including the ones we didnt double. Get modulo of the result from step If the answer is then the number is valid according to the Luhn Algorithm. Try writing out the algorithm with pen and paper before you start writing code. Input Format An ndigit credit card number, where the last digit is the check digit. Output Format Output "VALID" if it is a valid credit card number and "INVALID" if it is not. Constraints n Sample Input Sample Output VALID Explanation Make sure to read from right to left. First, add the numbers that we wont double: Now, double every second digit from the second last digit and subtract if needed. ; ; ; ; Adding these together So the card is VALID
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
