Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Outputting ordinal numbers (1st, 2nd, 3rd)

I would like to generate (as a return result of a function, or simply as the output of a program) the ordinal suffix of a positive integer concatenated to the number.

Samples:

1st 
2nd 
3rd 
4th 
... 
11th 
12th 
13th 
... 
20th 
21st
22nd
23rd
24th

And so on, with the suffix repeating the initial 1-10 subpattern every 10 until 100, where the pattern ultimately starts over.

The input would be the number and the output the ordinal string as shown above.

What is the smallest algorithm for this?

Answer*

Draft saved
Draft discarded
Cancel
3
  • \$\begingroup\$ A user flagged this saying "is incorrect: doesn't loop at all, i.e. it works just for a given number, not _all_ numbers below it. see ideone.com/pO6UwV ." That's not a reason to flag as moderators do not judge correctness, but it may be an issue. \$\endgroup\$ Commented Apr 11, 2013 at 0:00
  • \$\begingroup\$ Based upon this comment from the question creator: "@Ilmari I am looking for 11 as input and 11th as output. I don't mind if it processes multiple lines but what I had in mind was processing just a single number. – NickC Jan 20 '12 at 21:00" it is doing what it should. I.e. it only should process a single number. \$\endgroup\$ Commented Apr 11, 2013 at 4:42
  • \$\begingroup\$ sorry for that flag; I misread the requirement, and didn't have enough rep at the time, to comment directly. Sorry again. :) \$\endgroup\$ Commented Apr 13, 2013 at 19:48

AltStyle によって変換されたページ (->オリジナル) /