How to resolve the algorithm Luhn test of credit card numbers step by step in the PureBasic programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Luhn test of credit card numbers step by step in the PureBasic programming language

Table of Contents

Problem Statement

The Luhn test is used by some credit card companies to distinguish valid credit card numbers from what could be a random selection of digits. Those companies using credit card numbers that can be validated by the Luhn test have numbers that pass the following test:

For example, if the trial number is 49927398716:

Write a function/method/procedure/subroutine that will validate a number with the Luhn test, and use it to validate the following numbers:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Luhn test of credit card numbers step by step in the PureBasic programming language

Source code in the purebasic programming language

DataSection
  Sample:
  Data.s "49927398716"
  Data.s "49927398717"
  Data.s "1234567812345678"
  Data.s "1234567812345670"
  Data.s ""
EndDataSection

Procedure isValid(cardNumber.s)
  Protected i, length, s1, s2, s2a
  
  cardNumber = ReverseString(cardNumber)
  length = Len(cardNumber)
  For i = 1 To length Step 2
    s1 + Val(Mid(cardNumber, i, 1))
  Next 

  For i = 2 To length Step 2
    s2a = Val(Mid(cardNumber, i, 1)) * 2
    If s2a < 10
      s2 + s2a
    Else
      s2 + 1 + Val(Right(Str(s2a), 1))
    EndIf 
  Next 
  
  If Right(Str(s1 + s2), 1) = "0"
    ProcedureReturn #True
  Else
    ProcedureReturn #False
  EndIf 
EndProcedure


If OpenConsole()
  Define cardNumber.s
  
  Restore Sample
  Repeat 
    Read.s cardNumber
    If cardNumber <> ""
      Print(cardNumber + " is ")
      If isValid(cardNumber)
        PrintN("valid")
      Else
        PrintN("not valid")
      EndIf
    EndIf
  Until cardNumber = ""
  
  Print(#CRLF$ + #CRLF$ + "Press ENTER to exit")
  Input()
  CloseConsole()
EndIf

  

You may also check:How to resolve the algorithm Currency step by step in the Racket programming language
You may also check:How to resolve the algorithm Inconsummate numbers in base 10 step by step in the Action! programming language
You may also check:How to resolve the algorithm Sum of a series step by step in the Lambdatalk programming language
You may also check:How to resolve the algorithm Order two numerical lists step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Parsing/RPN calculator algorithm step by step in the Tcl programming language