luhn

How do I implement the Luhn algorithm?

梦想与她 提交于 2019-11-29 15:32:17
I am trying to create a program to validate 10 to 12 digit long number sequences based on the luhn algorithm, but my program keeps on telling me that every number is invalid even though they're not. This number should be valid, but my code doesn't think so: 8112189876 This number should not be valid, which my program agrees with, as it thinks every number is invalid: 8112189875 Here is my code: static void luhn(){ System.out.print("Enter number to validate:\n"); String pnr = input.nextLine(); int length = pnr.length(); int sum = 0; for (int i = 1, pos = length - 1; i < 10; i++, pos--){ char

Implementation of Luhn Formula

情到浓时终转凉″ 提交于 2019-11-29 15:19:39
I was trying to implement the Luhn Formula in Python, here is my code: import sys def luhn_check(number): if number.isdigit(): last_digit = int(str(number)[-1]) reverse_sequence = list(int(d) for d in str(int(number[-2::-1]))) for i in range(0, len(reverse_sequence), 2): reverse_sequence[i] *= 2 for i in range(len(reverse_sequence)): if reverse_sequence[i] > 9: reverse_sequence[i] -= 9 sum_of_digits = 0 for i in range(len(reverse_sequence)): sum_of_digits += reverse_sequence[i] result = divmod(sum_of_digits, 10) if result == last_digit: print("[VALID] %s" % number) else: print("[INVALID] %s" %

implementing luhn algorithm using c#

≯℡__Kan透↙ 提交于 2019-11-27 21:41:51
I am using following code to implement Luhn algorithm for credit card check in c# language but could not get the output to generate the check sum its showing validity: kindly help me.Thank you in advance public class Program { private static void Main(string[]creditcard) { int sum = 0, d; string num ="7992739871"; int a = 0; for (int i = num.Length - 2; i >= 0; i--) { d = Convert.ToInt32(num.Substring(i, 1)); if (a % 2 == 0) d = d * 2; if (d > 9) d -= 9; sum += d; a++; } if ((10 - (sum % 10) == Convert.ToInt32(num.Substring(num.Length - 1)))) Console.WriteLine("valid"); Console.WriteLine("sum

Check Credit Card Validity using Luhn Algorithm

浪子不回头ぞ 提交于 2019-11-27 13:19:36
I tried to check the validation of credit card using Luhn algorithm, which works as the following steps: Double every second digit from right to left. If doubling of a digit results in a two-digit number, add up the two digits to get a single-digit number. 2 * 2 = 4 2 * 2 = 4 4 * 2 = 8 1 * 2 = 2 6 * 2 = 12 (1 + 2 = 3) 5 * 2 = 10 (1 + 0 = 1) 8 * 2 = 16 (1 + 6 = 7) 4 * 2 = 8 Now add all single-digit numbers from Step 1. 4 + 4 + 8 + 2 + 3 + 1 + 7 + 8 = 37 Add all digits in the odd places from right to left in the card number. 6 + 6 + 0 + 8 + 0 + 7 + 8 + 3 = 38 Sum the results from Step 2 and Step

Check Credit Card Validity using Luhn Algorithm

橙三吉。 提交于 2019-11-26 16:17:58
问题 I tried to check the validation of credit card using Luhn algorithm, which works as the following steps: Double every second digit from right to left. If doubling of a digit results in a two-digit number, add up the two digits to get a single-digit number. 2 * 2 = 4 2 * 2 = 4 4 * 2 = 8 1 * 2 = 2 6 * 2 = 12 (1 + 2 = 3) 5 * 2 = 10 (1 + 0 = 1) 8 * 2 = 16 (1 + 6 = 7) 4 * 2 = 8 Now add all single-digit numbers from Step 1. 4 + 4 + 8 + 2 + 3 + 1 + 7 + 8 = 37 Add all digits in the odd places from