How to resolve the algorithm N'th step by step in the Oforth programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm N'th step by step in the Oforth 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 Oforth programming language

Source code in the oforth programming language

: nth(n)
| r |
   n "th" over 10 mod ->r
   r 1 == ifTrue: [ n 100 mod 11 == ifFalse: [ drop "st" ] ]
   r 2 == ifTrue: [ n 100 mod 12 == ifFalse: [ drop "nd" ] ]
   r 3 == ifTrue: [ n 100 mod 13 == ifFalse: [ drop "rd" ] ]  
   + ;

  

You may also check:How to resolve the algorithm User input/Text step by step in the PL/I programming language
You may also check:How to resolve the algorithm Loop over multiple arrays simultaneously step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Text processing/Max licenses in use step by step in the AWK programming language
You may also check:How to resolve the algorithm Roots of a quadratic function step by step in the Octave programming language
You may also check:How to resolve the algorithm Day of the week step by step in the XPL0 programming language