Programming ( Mathematical ) Practice for Fun:
Question:
An ISBN (International Standard Book Number) is a ten digit code which uniquely identifies a book. The first nine digits represent the book and the last digit is used to make sure the ISBN is correct. To verify an ISBN you calculate 10 times the first digit, plus 9 times the second digit, plus 8 times the third ... all the way until you add 1 times the last digit. If the final number leaves no remainder when divided by 11 the code is a valid ISBN.
For example: 0201103311 is a valid ISBN, since
10*0 + 9*2 + 8*0 + 7*1 + 6*1 + 5*0 + 4*3 + 3*3 + 2*1 + 1*1 = 55.
Each of the first nine digits can take a value between 0 and 9. Sometimes it is necessary to make the last digit equal to ten; this is done by writing the last digit as X. For example: 156881111X.
Write a program that reads in a valid ISBN with a single missing digit, marked with a “ ? “ , and outputs the correct value for the missing digit.
Sample Input Sample Output
ISBN: 15688?111X Missing Digit: 1
Solution:
Visual Basic
Module Module1
Sub Main()
Dim ISBNcode As String
Dim total1, temp, total2 As Integer
Dim counter1 As Integer = 1
Console.Write("ISBN: ")
ISBNcode = Console.ReadLine()
Dim digit(10) As String
Dim counter As Integer
For counter = 1 To Len(ISBNcode)
digit(counter) = Mid(ISBNcode, counter, 1)
Next
If digit(10) = "X" Then digit(10) = "10"
For counter = 10 To 1 Step -1
If digit(counter1) = "?" Then
temp = counter
counter1 = counter1 + 1
Else
total1 = total1 + Val(digit(counter1)) * counter
counter1 = counter1 + 1
End If
Next
For counter = 0 To 10
total2 = temp * counter + total1
If total2 Mod 11 = 0 And counter = 10 Then
Console.WriteLine("Missing Digit: X")
Else
If total2 Mod 11 = 0 Then Console.WriteLine("Missing Digit: " & counter)
End If
Next
Console.ReadKey()
End Sub
End Module