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

Source code in the objeck programming language

bundle Default {
  class Luhn {
    function : IsValid(cc : String) ~ Bool {
      isOdd := true;
      oddSum := 0;
      evenSum := 0;
      
      for(i := cc->Size() - 1; i >= 0; i -= 1;) {
        digit : Int := cc->Get(i) - '0';
        if(isOdd) {
          oddSum += digit;
        } 
        else {
          evenSum += digit / 5 + (2 * digit) % 10;
        };
        isOdd := isOdd <> true;
      };
       
      return (oddSum + evenSum) % 10 = 0;
    }
    
    function : Main(args : String[]) ~ Nil {
      IsValid("49927398716")->PrintLine();
      IsValid("49927398717")->PrintLine();
      IsValid("1234567812345678")->PrintLine();
      IsValid("1234567812345670")->PrintLine();
    }
  }
}

  

You may also check:How to resolve the algorithm Sorting algorithms/Gnome sort step by step in the Ada programming language
You may also check:How to resolve the algorithm Lah numbers step by step in the Perl programming language
You may also check:How to resolve the algorithm Anagrams/Deranged anagrams step by step in the Lasso programming language
You may also check:How to resolve the algorithm Symmetric difference step by step in the PARI/GP programming language
You may also check:How to resolve the algorithm Partial function application step by step in the Go programming language