FIXED
Old Post
Old Post (Click to show)Okies
Basically This is a function to verify if a 13 digit ISBN number is a valid ISBN
The algorithm gets the digit sum of all the odd digits and the sum of all the even digits *3
The total sum is then mod by 10 and if (10 - remainder = last digit ) then it is valid
Input Number
9780306406157
Correct Odd sum = 27 My Odd sum = 31
Correct Even sum = 66 My Even sum = 66

Correct Total sum = 93 My Total sum = 97

Correct Remainder = 3 My Remainder = 7
Code (Click to show)Function ISBN13() As Boolean
Dim CharArr(0 To 12) As Integer
Dim TotalSum As Integer
Dim counter As Integer
counter = 0
'Gets the Sum Of All The Odd Digits In The ISBN Number'
Dim OddSum As Integer
'Calculates Sum of Even digits'
Dim EvenSum As Integer
EvenSum = 0
'9780306406157'
'1 3 5 7 9 11'
'The loop ignores the first digit for some reason'
OddSum = OddSum + (Microsoft.VisualBasic.Val(IsbnTxt.Text(0)))
For counter = 0 To 11
If (counter Mod 2 = 1) Then
OddSum = OddSum + (Microsoft.VisualBasic.Val(IsbnTxt.Text(counter)))
End If
If ((counter) Mod 2 = 0) Then 'And Not (counter = IsbnTxt.TextLength -)' Then
EvenSum = EvenSum + (Microsoft.VisualBasic.Val(IsbnTxt.Text(counter + 1)) * 3)
End If
Next counter
MessageBox.Show("Odd" + Convert.ToString(OddSum))
MessageBox.Show("Even" + Convert.ToString(EvenSum))
TotalSum = EvenSum + OddSum
MessageBox.Show("Total" + Convert.ToString(TotalSum))
Dim Remainder As Integer
Remainder = TotalSum Mod 10
MessageBox.Show("Remainder" + Convert.ToString(Remainder))
If 10 - Remainder = Convert.ToInt32(IsbnTxt.Text(12)) Then Return True
Exit Function
End Function
Rewrote code in loop
New Code if anybody wants
Somehow it was adding the last digit twice and skipping the first
Code (Click to show)Function ISBN13() As Boolean
If IsbnTxt.TextLength <> 13 Then
IsbnLbl.BackColor = Color.Red
Return False
End If
Dim TotalSum As Integer
Dim counter As Integer
counter = 0
'Gets the Sum Of All The Odd Digits In The ISBN Number'
Dim OddSum As Integer
'Calculates Sum of Even digits'
Dim EvenSum As Integer
EvenSum = 0
'9780306406157'
'1 3 5 7 9 11'
'The loop ignores the first digit for some reason'
OddSum = OddSum + (Microsoft.VisualBasic.Val(IsbnTxt.Text(0)))
OddSum = OddSum - (Microsoft.VisualBasic.Val(IsbnTxt.Text(12)))
For counter = 0 To 11
If (counter Mod 2 = 1) Then
OddSum = OddSum + (Microsoft.VisualBasic.Val(IsbnTxt.Text(counter + 1)))
'MessageBox.Show("Odd counter" + counter.ToString)
'MessageBox.Show("Odd ISBN " + IsbnTxt.Text(counter + 1))
'MessageBox.Show("Odd sum in loop " + OddSum.ToString)
End If
If ((counter) Mod 2 = 0) Then
EvenSum = EvenSum + (Microsoft.VisualBasic.Val(IsbnTxt.Text(counter + 1)) * 3)
'MessageBox.Show("Even counter " + counter.ToString)
'MessageBox.Show("Even ISBN " + IsbnTxt.Text(counter + 1))
End If
Next counter
TotalSum = EvenSum + OddSum
Dim Remainder As Integer
Remainder = TotalSum Mod 10
Dim Checkdigit As Integer
Checkdigit = 10 - Remainder
If Checkdigit.ToString = IsbnTxt.Text(12) Then
IsbnLbl.BackColor = Color.Lime
Return True
End If
If Checkdigit.ToString <> IsbnTxt.Text(12) Then
IsbnLbl.BackColor = Color.Orange
Return False
End If
Exit Function
End Function
Edited by nukefission - 9/15/12 at 7:04am