How to resolve the algorithm Luhn test of credit card numbers step by step in the BASIC 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 BASIC 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 BASIC programming language

Source code in the basic programming language

dim card$(5)
card$[1]="49927398716"
card$[2]="49927398717"
card$[3]="1234567812345678"
card$[4]="1234567812345670"

for test = 1 to 4
    odd = True
    sum = 0
    for n = length(card$[test]) to 1 step -1
        num = int(mid(card$[test],n,1))
        if odd then
            sum += num
            odd = False
        else
            num *= 2
            if num <= 9 then
                sum += num
            else
                sum += int(left(string(num),1)) + int(right(string(num),1))
            end if
            odd = True
        end if
    next
    if sum mod 10 = 0 then
        print card$[test], "True"
    else
        print card$[test], "False"
    end if
next test

100 cls
110 rem Luhn test
120 dim card$(5)
130 card$(1) = "49927398716"
140 card$(2) = "49927398717"
150 card$(3) = "1234567812345678"
160 card$(4) = "1234567812345670"
170 for test = 1 to 4
180   odd = true
190   sum = 0
200   for n = len(card$(test)) to 1 step -1
210     num = val(mid$(card$(test),n,1))
220     if odd then
230       sum = sum+num
240       odd = false
250     else
260       num = num*2
270       if num <= 9 then
280         sum = sum+num
290       else
300         sum = sum+val(left$(str$(num),1))+val(right$(str$(num),1))
310       endif
320       odd = true
330     endif
340   next
350   if sum mod 10 = 0 then
360     print card$(test),"True"
370   else
380     print card$(test),"False"
390   endif
400 next test


100 PROGRAM "CredCard.bas"
110 DO
120   PRINT :PRINT "Credit card number:":INPUT PROMPT ">":CCN$
130   IF CCN$="" THEN EXIT DO
140   IF LUHN(TRIM$(CCN$)) THEN
150     PRINT "Card number is valid."
160   ELSE
170     SET #102:INK 3:PRINT "Card number is invalid.":SET #102:INK 1
180   END IF
190 LOOP
200 DEF LUHN(CCN$)
210   LET L=LEN(CCN$):LET S=0
220   FOR I=1 TO L
230     LET N=VAL(CCN$(L-I+1))
240     IF I BAND 1 THEN
250       LET S=S+N
260     ELSE
270       LET N=N*2:LET S=S+MOD(N,10)+INT(N/10)
280     END IF
290   NEXT
300   LET LUHN=MOD(S,10)=0
310 END DEF
320 DEF TRIM$(S$)
330   LET T$=""
340   FOR I=1 TO LEN(S$)
350     IF S$(I)>CHR$(47) AND S$(I)<CHR$(58) THEN LET T$=T$&S$(I)
360   NEXT
370   LET TRIM$=T$
380 END DEF

CONST True = -1: False = NOT True

DIM card$(5)
card$(1) = "49927398716"
card$(2) = "49927398717"
card$(3) = "1234567812345678"
card$(4) = "1234567812345670"

FOR test = 1 TO 4
    odd = True
    sum = 0
    FOR n = LEN(card$(test)) TO 1 STEP -1
        num = VAL(MID$(card$(test), n, 1))
        IF odd THEN
           sum = sum + num
           odd = False
        ELSE
           num = num * 2
           IF num <= 9 THEN
              sum = sum + num
           ELSE
              sum = sum + VAL(LEFT$(STR$(num), 1)) + VAL(RIGHT$(STR$(num), 1))
           END IF
           odd = True
        END IF
    NEXT
    IF sum MOD 10 = 0 THEN
       PRINT card$(test), "True"
    ELSE
       PRINT card$(test), "False"
    END IF
NEXT test


LET true = -1
LET false = 0

DIM card$(5)
LET card$(1) = "49927398716"
LET card$(2) = "49927398717"
LET card$(3) = "1234567812345678"
LET card$(4) = "1234567812345670"

FOR test = 1 TO 4
    LET odd = true
    LET sum = 0
    FOR n = LEN(card$(test)) TO 1 STEP -1
        LET num = VAL((card$(test))[n:n+1-1])
        IF odd<>0 THEN
           LET sum = sum + num
           LET odd = false
        ELSE
           LET num = num*2
           IF num <= 9 THEN
              LET sum = sum + num
           ELSE
              LET sum = sum + VAL((STR$(num))[1:1]) + VAL((STR$(num))[LEN(STR$(num))-1+1:maxnum])
           END IF
           LET odd = true
        END IF
    NEXT n
    IF remainder(round(sum),10) = 0 THEN PRINT card$(test), "True" ELSE PRINT card$(test), "False"
NEXT test
END


Print "     49927398716", Show (Iif(FUNC(_Luhn ("49927398716")), "ok", "fail"))
Print "     49927398717", Show (Iif(FUNC(_Luhn ("49927398717")), "ok", "fail"))
Print "1234567812345678", Show (Iif(FUNC(_Luhn ("1234567812345678")), "ok", "fail"))
Print "1234567812345670", Show (Iif(FUNC(_Luhn ("1234567812345670")), "ok", "fail"))
End

_Luhn
  Param (1)
  Local (4)

  c@ = 1 : d@ = 0

  For b@ = Len(a@)-1 To 0 Step -1
    e@ = Peek(a@, b@) - Ord("0")
    d@ = d@ + Iif (c@, e@, e@+e@-9*(e@>4))
    c@ = c@ = 0
  Next

Return ((d@ % 10) = 0)


dim card$(5)
card$(1)="49927398716"
card$(2)="49927398717"
card$(3)="1234567812345678"
card$(4)="1234567812345670"

for test = 1 to 4
    odd = true
    sum = 0
    for n = len(card$(test)) to 1 step -1
        num = val(mid$(card$(test),n,1))
        if odd then
            sum = sum + num
            odd = false
        else
            num = num * 2
            if num <= 9 then
                sum = sum + num
            else
                sum = sum + val(left$(str$(num),1)) + val(right$(str$(num),1))
            fi
            odd = true
        fi
    next
    if mod(sum, 10) = 0 then
        print card$(test), chr$(9), "True"
    else
        print card$(test), chr$(9), "False"
    fi
next test

  

You may also check:How to resolve the algorithm Anti-primes step by step in the Red programming language
You may also check:How to resolve the algorithm Short-circuit evaluation step by step in the Elixir programming language
You may also check:How to resolve the algorithm Descending primes step by step in the EasyLang programming language
You may also check:How to resolve the algorithm Product of min and max prime factors step by step in the jq programming language
You may also check:How to resolve the algorithm Gray code step by step in the Ada programming language