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

Source code in the clu programming language

luhn = proc (num: string) returns (bool) signals (bad_format)
    total: int := 0
    even: bool := true
    for i: int in int$from_to_by(string$size(num), 1, -1) do
        digit: int := int$parse(string$c2s(num[i])) resignal bad_format
        even := ~even
        if even then
            digit := 2 * digit
            if digit >= 10 then digit := digit//10 + 1 end
        end
        total := total + digit
    end
    return(total // 10 = 0)
end luhn

start_up = proc ()
    po: stream := stream$primary_output()
    tests: sequence[string] := sequence[string]$
        ["49927398716", "49927398717", "1234567812345678", "1234567812345670"]
    for test: string in sequence[string]$elements(tests) do
        stream$puts(po, test || ": ")
        if luhn(test)
            then stream$putl(po, "pass")
            else stream$putl(po, "fail")
        end
    end
end start_up

  

You may also check:How to resolve the algorithm Empty program step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Loop over multiple arrays simultaneously step by step in the LFE programming language
You may also check:How to resolve the algorithm Magic squares of odd order step by step in the REXX programming language
You may also check:How to resolve the algorithm Terminal control/Coloured text step by step in the PARI/GP programming language
You may also check:How to resolve the algorithm Mutual recursion step by step in the FOCAL programming language