How to resolve the algorithm CUSIP step by step in the Ada programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm CUSIP step by step in the Ada programming language

Table of Contents

Problem Statement

A   CUSIP   is a nine-character alphanumeric code that identifies a North American financial security for the purposes of facilitating clearing and settlement of trades. The CUSIP was adopted as an American National Standard under Accredited Standards X9.6.

Ensure the last digit   (i.e., the   check digit)   of the CUSIP code (the 1st column) is correct, against the following:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm CUSIP step by step in the Ada programming language

Source code in the ada programming language

with Ada.Text_IO;

procedure Cusip_Test is
   use Ada.Text_IO;

   subtype Cusip is String (1 .. 9);

   function Check_Cusip (Code : Cusip) return Boolean is
      Sum : Integer := 0;
      V   : Integer;

   begin
      for I in Code'First .. Code'Last - 1 loop
         case Code (I) is
            when '0' .. '9' =>
               V := Character'Pos (Code (I)) - Character'Pos ('0');
            when 'A' .. 'Z' =>
               V := Character'Pos (Code (I)) - Character'Pos ('A') + 10;
            when '*' => V := 36;
            when '@' => V := 37;
            when '#' => V := 38;
            when others => return False;
         end case;

         if I mod 2 = 0 then
            V := V * 2;
         end if;

         Sum := Sum + V / 10 + (V mod 10);
      end loop;

      return (10 - (Sum mod 10)) mod 10 =
        Character'Pos (Code (Code'Last)) - Character'Pos ('0');
   end Check_Cusip;

   type Cusip_Array is array (Natural range <>) of Cusip;

   Test_Array : Cusip_Array :=
     ("037833100",
      "17275R102",
      "38259P508",
      "594918104",
      "68389X106",
      "68389X105");
begin
   for I in Test_Array'Range loop
      Put (Test_Array (I) & ": ");
      if Check_Cusip (Test_Array (I)) then
         Put_Line ("valid");
      else
         Put_Line ("not valid");
      end if;
   end loop;
end Cusip_Test;


  

You may also check:How to resolve the algorithm Integer sequence step by step in the Delphi programming language
You may also check:How to resolve the algorithm Mutual recursion step by step in the Eiffel programming language
You may also check:How to resolve the algorithm Arithmetic evaluation step by step in the BBC BASIC programming language
You may also check:How to resolve the algorithm Arithmetic/Integer step by step in the C programming language
You may also check:How to resolve the algorithm Command-line arguments step by step in the Raven programming language