How to resolve the algorithm Luhn test of credit card numbers step by step in the XPL0 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 XPL0 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 XPL0 programming language
Source code in the xpl0 programming language
string 0; \use zero-terminated strings
func Valid(Str); \Return 'true' if digits in Str pass Luhn test
char Str;
int Len, Sum, I, Dig;
[Len:= 0; \find length of Str
while Str(Len) do Len:= Len+1;
Sum:= 0; \sum even and odd digits
for I:= 0 to Len-1 do \(no need to reverse)
[if (I xor Len) and 1 then
Sum:= Sum + Str(I) - ^0
else [Dig:= Str(I) - ^0;
Dig:= Dig*2;
Sum:= Sum + Dig/10 + rem(0);
];
];
return rem(Sum/10) = 0;
];
int Luhn, N;
[Luhn:= ["49927398716",
"49927398717",
"1234567812345678",
"1234567812345670"];
for N:= 0 to 4-1 do
[Text(0, Luhn(N));
Text(0, if Valid(Luhn(N))
then " is valid"
else " is not valid");
CrLf(0);
];
]
You may also check:How to resolve the algorithm Caesar cipher step by step in the Swift programming language
You may also check:How to resolve the algorithm Compile-time calculation step by step in the Rust programming language
You may also check:How to resolve the algorithm Abundant, deficient and perfect number classifications step by step in the Batch File programming language
You may also check:How to resolve the algorithm Soundex step by step in the RPL programming language
You may also check:How to resolve the algorithm Function definition step by step in the Nim programming language