Microsoft Small Basic

Program Listing: TLN677
' Credit Card Checking Program
TextWindow.Write ("Please enter your credit card number")
CreditCardNumber = TextWindow.Read()

'Initialize the variable for the total and step through the string entered
total = 0
For index = 1 To 15 Step 2
' Take the digits two at a time
digit = Text.GetSubText(CreditCardNumber, index, 1)
digit2 = Text.GetSubText(CreditCardNumber, index + 1, 1)

'Odd positioned digits are doubled - added if less than 10 or add the resulting digits if greater than 10
If (digit * 2 >= 10) Then
total = total + Text.GetSubText((digit *2), 2, 1) + 1
Else
total = total + digit * 2
EndIf

'Even positioned digits are just added to the total
total = total + digit2
EndFor

'Output the checksum
TextWindow.Write ("Checksum is: ")
TextWindow.WriteLine (total)

'Output an appropriate message
If (Text.GetSubText(total, 2, 1)) = 0 Then
TextWindow.WriteLine ("This is a valid Credit Card Number")
Else
TextWindow.WriteLine ("This is an invalid Credit Card Number")
EndIf