This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Hello,

I am validating a credit card with the check can be described as follows.

For the number 49927389716

Reverse the digits: 61798372994

Sum the digits as odd positions: 6 + 7 + 9 + 7 + 9 + 4 = 42 = s1

The even positioned digits: 1, 8, 3, 2, 9 Multiply each even positioned digit by 2: 2, 16, 6, 4, 18

Sum the digits of each multiplication: 2, 7, 6, 4, 9 And them sum them: 2 + 7 + 6 + 4 + 9 = 28 = s2

Add s1 and s2 and perform mod 10 s1 + s2 = 70

which ends in zero which means that 49927398716 passes the Luhn test

Any suggestion on a good code here

I am validating a credit card with the check can be described as follows.

For the number 49927389716

Reverse the digits: 61798372994

Sum the digits as odd positions: 6 + 7 + 9 + 7 + 9 + 4 = 42 = s1

The even positioned digits: 1, 8, 3, 2, 9 Multiply each even positioned digit by 2: 2, 16, 6, 4, 18

Sum the digits of each multiplication: 2, 7, 6, 4, 9 And them sum them: 2 + 7 + 6 + 4 + 9 = 28 = s2

Add s1 and s2 and perform mod 10 s1 + s2 = 70

which ends in zero which means that 49927398716 passes the Luhn test

```
Private Function ValidateLuhn(ByVal value As String) As Boolean
02
03
Dim CheckSum As Integer = 0
04
Dim DoubleFlag As Boolean = (value.Length Mod 2 = 0)
05
06
Dim Digit As Char
07
Dim DigitValue As Integer
08
For Each Digit In value
09
DigitValue = Integer.Parse(Digit)
10
If DoubleFlag Then
11
DigitValue *= 2
12
If DigitValue > 9 Then
13
DigitValue -= 9
14
End If
15
End If
16
CheckSum += DigitValue
17
DoubleFlag = Not DoubleFlag
18
Next
19
20
Return (CheckSum Mod 10 = 0)
21
22
End Function
```

Any suggestion on a good code here

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with Premium.
Start your 7-day free trial.

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Thanks for the advise but I get tese errors:

Error 1 'LuhnValidation' is not a member of 'String'

Error 2 'All' is not a member of 'String'.

Error 3 Expression expected.

Error 4 'Select' is not a member of 'String'.

Error 5 Expression expected

Error 6 Name 'i' is not declared.

Error 7 Name 'c' is not declared.

Error 8 Name 'i' is not declared.

Error 9 End of statement expected.

```
Public Function LuhnValidation(ByVal source As String) As Boolean
Dim result As Boolean = False
Try
' We should validate that we are dealing with integers.
If source.All(Function(c) Char.IsNumber(c)) Then
' Rules of algorithm
' 1. Reverse the order of the digits in the number.
' 2. Take the first, third, ... and every other odd digit in the reversed digits and sum them to form the partial sum s1
' 3. Taking the second, fourth ... and every other even digit in the reversed digits:
' a. Multiply each digit by two and sum the digits if the answer is greater than nine to form partial sums for the even digits
' b. Sum the partial sums of the even digits to form s2
' 4. If s1 + s2 ends in zero then the original number is in the form of a valid credit card number as verified by the Luhn test.
' Code from - http://rosettacode.org/wiki/Luhn_test_of_credit_card_numbers#Visual_Basic_.NET
result = source.Select(Function(c, i) (AscW(c) - 48) << ((source.Length - i - 1) And 1)).Sum(Function(n) If(n > 9, n - 9, n)) Mod 10 = 0
End If
Catch ex As Exception
result = False
End Try
Return result
End Function
```

What .NET version are you using? If 3.5 or above, try adding an Import for System.Linq to the top of your form code.

-saige-

```
Private Function ValidateLuhn(cardnumber As String) As Boolean
Dim _charArray As Char() = cardnumber.ToCharArray()
Array.Reverse(_charArray)
Dim _odds As Integer = 0, _evens As Integer = 0, _num As Integer, _tmp As Integer
Dim _valid As Boolean = False
For _index As Integer = 0 To _charArray.Length() - 1
_num = Integer.Parse(_charArray(_index))
If (_index + 1) Mod 2 = 0 Then
_tmp = _num * 2
If _tmp > 10 Then
_evens += Integer.Parse(_tmp.ToString().Substring(0, 1)) + Integer.Parse(_tmp.ToString().Substring(1, 1))
Else
_evens += _tmp
End If
Else
_odds += _num
End If
Next
_num = _odds + _evens
Return ((_num Mod 10) = 0)
End Function
```

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial```
Private Function LuhnValidation(ByVal source As String) As Boolean
Dim result As Boolean = False
Try
' We should validate that we are dealing with integers.
If Not IsNumeric(source) Then Exit Function
' Rules of algorithm
' 1. Reverse the order of the digits in the number.
' 2. Take the first, third, ... and every other odd digit in the reversed digits and sum them to form the partial sum s1
' 3. Taking the second, fourth ... and every other even digit in the reversed digits:
' a. Multiply each digit by two and sum the digits if the answer is greater than nine to form partial sums for the even digits
' b. Sum the partial sums of the even digits to form s2
' 4. If s1 + s2 ends in zero then the original number is in the form of a valid credit card number as verified by the Luhn test.
Dim sum As Integer = 0
For i As Integer = source.Length - 1 To 0 Step -1
If i Mod 2 <> 0 Then
sum = sum + (AscW(source(i)) - 48)
Else
If ((AscW(source(i)) - 48) * 2) > 9 Then
sum = sum + ((((AscW(source(i)) - 48) * 2) \ 10) + (((AscW(source(i)) - 48) * 2) - 10))
Else
sum = sum + ((AscW(source(i)) - 48) * 2)
End If
End If
Next
result = sum Mod 10 = 0
Catch ex As Exception
result = False
End Try
Return result
End Function
```

-saige-
Visual Basic.NET

From novice to tech pro — start learning today.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with Premium.
Start your 7-day free trial.

Open in new window

-saige-