How to resolve the algorithm N'th step by step in the PL/I programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm N'th step by step in the PL/I programming language

Table of Contents

Problem Statement

Write a function/method/subroutine/... that when given an integer greater than or equal to zero returns a string of the number followed by an apostrophe then the ordinal suffix.

Returns would include 1'st 2'nd 3'rd 11'th 111'th 1001'st 1012'th

Use your routine to show here the output for at least the following (inclusive) ranges of integer inputs: 0..25, 250..265, 1000..1025

Note: apostrophes are now optional to allow correct apostrophe-less English.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm N'th step by step in the PL/I programming language

Source code in the pl/i programming language

Nth: procedure options (main);   /* 1 June 2014 */
   declare i fixed (10);

   do i = 0 to 25, 250 to 265, 1000 to 1025;
      if i = 250 | i = 1000 then put skip (2);
      put edit (enth(i)) (x(1), a);
   end;

enth: procedure (i) returns (character (25) varying);
   declare i fixed (10);
   declare suffix character (2);

   select (mod(i, 10));
      when (1)  suffix = 'st';
      when (2)  suffix = 'nd';
      when (3)  suffix = 'rd';
      otherwise suffix = 'th';
   end;
   select (mod(i, 100));
      when (11, 12, 13) suffix = 'th';
      otherwise ;
   end;
   return ( trim(i) || suffix );
end enth;

end Nth;


  

You may also check:How to resolve the algorithm Short-circuit evaluation step by step in the PL/I programming language
You may also check:How to resolve the algorithm Variadic function step by step in the PL/I programming language
You may also check:How to resolve the algorithm Range expansion step by step in the PL/I programming language
You may also check:How to resolve the algorithm Flipping bits game step by step in the PL/I programming language
You may also check:How to resolve the algorithm Loops/N plus one half step by step in the PL/I programming language