g | x | w | all
Bytes Lang Time Link
061AWK250227T204900Zxrs
074Tcl250225T123229Zsergiol
041J140301T220552Zalgorith
110Go220925T175352Zbigyihsu
006Vyxal220925T035616ZnaffetS
644Javascript ES6 44 Bytes160225T165252ZShaun H
049Python 2160224T120820Zxsot
313brainfuck220210T043706Zengineer
014Vyxal220202T230740Zemanresu
nanScratch210619T164120Zsmarnav
023Vyxal210619T065227ZUndersla
058Bash210618T121638Zuser1004
087C210618T055950Zei2
041FEU200710T114822ZPkmnQ
076APLNARS200706T205945Zuser5898
091Bash Cardinal to ordinal 91 Bytes200505T090051Zstephanm
02105AB1E200706T120545ZKevin Cr
100Haskell130404T162031ZWill Nes
060Javascript130409T104930ZInkbug
036APL Dyalog Unicode160224T235908ZAdá
076R180503T173542ZJayCe
068Julia 0.6180503T165305Zniczky12
051JavaScript Node.js180503T141808Zuser8030
039Procedural Footnote Language180503T133928Zvasilesc
nanJavaScript170508T122156ZTomas La
033Pyth170515T025921Zclapp
063Common Lisp161226T075007Zhabs
081Java 7161108T124725ZKevin Cr
034Mathematica 29 + 5 =161108T054354ZJungHwan
151Scala 2.11.7160923T121335ZAbel Ter
062C#160928T120013Zadrianmp
084Python160225T155257ZNoOneIsH
083Haxe150323T214331Zcsomakk
052Javascript ES6160225T210831ZQwertiy
101Oracle SQL 11.2160225T115622ZJeto
098PHP160224T162006Zricdesi
045Mathematica130404T145249ZDavidC
075Javascript150324T195402Zwolfhamm
083C150324T191730Zpawel.bo
044K140306T233023Zalgorith
6871Javascript140213T235829Zaebabis
nan120121T094611ZJoseph E
095C130404T234111ZFors
086Scala120429T003313Zuser unk
151PHP120428T215702Zl0n3sh4r
034Golfscript120128T231535Zhammar
092PowerShell120123T102212ZJoey
137Python 2.7120123T055333ZBlazer
068Python120121T002319ZGareth
371Perl120120T184717ZIlmari K
060Ruby120120T230004ZMr. Llam

AWK, 61 bytes

$0=$0($0<9||$0>14?$NF~1?"st":$NF~2?"nd":$NF~3?"rd":"th":"th")

Attempt This Online!

Tcl, 74 bytes

proc o n {puts $n[lindex {th st nd rd} [expr $n%100-11&&$n%10<4?$n%10:0]]}

Try it online!

J -  44  41 char

Nothing in J? This is an outrage!

(":,th`st`nd`rd{::~4|4<.10(|*|~:-~)100&|)

Explained (note that 1 is boolean true in J and 0 is false):

Usage is obvious, though as-is the verb can only take one ordinal, not a list.

   (":,th`st`nd`rd{::~4|4<.10(|*|~:-~)100&|) 112         NB. single use
112th
   (":,th`st`nd`rd{::~4|4<.10(|*|~:-~)100&|) 1 2 3 4 5   NB. doing it wrong
|length error
|       (":,th`st`nd`rd{::~4|4<.10(|*|~:-~)100&|)1 2 3 4 5
   NB. i.5 10   makes a 5x10 grid of increasing integers
   NB. &.>      to operate on each integer separately, and box the result after
   (":,th`st`nd`rd{::~4|4<.10(|*|~:-~)100&|)&.> i.5 10   NB. all better
+----+----+----+----+----+----+----+----+----+----+
|0th |1st |2nd |3rd |4th |5th |6th |7th |8th |9th |
+----+----+----+----+----+----+----+----+----+----+
|10th|11th|12th|13th|14th|15th|16th|17th|18th|19th|
+----+----+----+----+----+----+----+----+----+----+
|20th|21st|22nd|23rd|24th|25th|26th|27th|28th|29th|
+----+----+----+----+----+----+----+----+----+----+
|30th|31st|32nd|33rd|34th|35th|36th|37th|38th|39th|
+----+----+----+----+----+----+----+----+----+----+
|40th|41st|42nd|43rd|44th|45th|46th|47th|48th|49th|
+----+----+----+----+----+----+----+----+----+----+

Alternative 41 char solution showing off a couple logical variants:

(":;@;st`nd`rd`th{~3<.10(|+3*|=-~)100|<:)

Previously I had an overlong explanation of this 44 char hunk of junk. 10 10#: takes the last two decimal digits and /@ puts logic between them.

(":,th`st`nd`rd{::~10 10(]*[(~:*])4>])/@#:])

Go, 173 110 bytes

import."fmt"
func o(n int){s,k:="th",n%10
if k>0&&k<4&&n/10%10!=1{s=[]string{"st","nd","rd"}[k-1]}
Print(n,s)}

Attempt This Online!

Old answer, 173 bytes

import."fmt"
func o(n int)string{s:=Sprint(n)
switch n%100{case 11,12,13:s+="th"
default:switch n%10{case 1:s+="st"
case 2:s+="nd"
case 3:s+="rd"
default:s+="th"}}
return s}

Attempt This Online!

Vyxal, 6 bytes

∆o2NȯJ

Try it Online!

Javascript (ES6) 50 44 Bytes

a=>a+=[,"st","nd","rd"][a.match`1?.$`]||"th"

Notes

Python 2, 49 bytes

lambda n:`n`+'tsnrhtdd'[n%5*(n%100^15>4>n%10)::4]

An anonymous function. A full program would be counted at 55 bytes.

'tsnrhtdd'[i::4] encodes the suffixes th st nd rd for values of i from 0 to 3. Given this, all we need is a way to map the values of n to the index of the corresponding suffix, i. A straightforward expression that works is (n%10)*(n%10<4 and not 10<n%100<14). We can easily shorten this by dropping the first set of parentheses and observing that n%5 gives the same results as n%10 for the values of n with the special suffixes. With a bit of trial and error, one may also shorten not 10<n%100<14 to n%100^15>4, which can be chained with the other conditional to save even more bytes.

brainfuck, 313 bytes

This was painful. Setup could probably be golfed better. May write a half-decent explanation later.

++++[>++++++<-]>-[<+++++>-]<[->+>+>+>+>+>+>+>+<<<<<<<<]>->--------------->----->--------------->>+>+>----------->>>>+>+>+>,[.>,]<<<[-]>>>-[>+<-----]>---[-<<-<->>>]<<<-[[-]>[->+>++<<]>+<+++++[->-[>]<<<]>[<<<]>[>>[-<<<<<[<]<<<<<[>]<[[->+<]<]>>>>>>>>>[-<<<<<<<<+>>>>>>>>]>>>>>[>]>>>>]<<<]]<<<[<]<<[<]<<<[<]>>>>>>>.>.

Try it online!

Vyxal, 14 bytes

ÞoÞ∞ZƛhṘ2ẎṘntp

Try it Online!

ÞoÞ∞Z          # Zip ordinals with positive integers
     ƛ         # Over each
      hṘ2ẎṘ    # Get the last two characters of the ordinal
           ntp # Append the number

Scratch, idk bytes

Scratch!

I tried to convert it to sb syntax, but it did not work

Try it!

Please comment if you know a way to golf, or byte-count this

Vyxal, 23 bytes

₀%:4<?₀ḭċ∧*4«∨¦İk√«/$i+

Try it Online!

Explanation:
₀%:4<?₀ḭċ∧*4«∨¦İk√«/$i+    # full program

₀%:                         # push input mod 10 twice
   4<                       # less than 4
     ?₀ḭ                    # input floor div 10
        ċ                   # != 1
         ∧*                 # and both bools, then multiply by initial mod 10
            4«∨¦İk√«/       # "thstndrd" split into 4 parts
                      $i    # indexed by input mod 10
                        +   # concat

Bash, 58 bytes

Just a modification of @stephanmg's post in Bash. Output is on STDERR as in "command not found".

case $1 in
*1?|*[04-9])$1th;;*1)$1st;;*2)$1nd;;*)$1rd
esac

Try it online!

This program completely relies on the limitation that the input is a non-negative decimal integer.

C, 87 bytes (correct teen handling)

This C solution is longer than the current shortest 83 byte C solution posted, but as of writing, that 83 byte solution is incorrect and prints numbers like 11st, 12nd, and 13rd.

Here is my solution, which correctly handles teen numbers:

main(n){scanf("%d",&n);printf("%d%s",n,"th\0st\0nd\0rd"+(n%10<4&&n/10%10-1?n%10*3:0));}

Degolfed:

main(n) {
    scanf("%d", &n);
    printf("%d%s", n, "th\0st\0nd\0rd" + (n%10 < 4 && n/10%10 - 1 ? n%10*3 : 0));
}

FEU, 41 bytes

m/1.$/\0th/1$/1st/2$/2nd/3$/3rd/\d$/\0th/

Try it online!

APL(NARS), 38 chars, 76 bytes

(⍕,{((⍳3)⍳10∣⍵×3≠⌊√⍵)⊃'snrt',¨'tddh'})

test:

  h←(⍕,{((⍳3)⍳10∣⍵×3≠⌊√⍵)⊃'snrt',¨'tddh'})
  h¨0..19
 0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 
  h¨20..31
 20th 21st 22nd 23rd 24th 25th 26th 27th 28th 29th 30th 31st 
  ⎕fmt h¨100..104
┌5───────────────────────────────────────────┐
│┌5─────┐ ┌5─────┐ ┌5─────┐ ┌5─────┐ ┌5─────┐│
││ 100th│ │ 101st│ │ 102nd│ │ 103rd│ │ 104th││
│└──────┘ └──────┘ └──────┘ └──────┘ └──────┘2
└∊───────────────────────────────────────────┘

Bash (Cardinal to ordinal) 91 Bytes:

function c(){ case "$1" in *1[0-9]|*[04-9])"$1"th;;*1)"$1"st;;*2)"$1"nd;;*3)"$1"rd;;esac }

05AB1E, 21 bytes

т%T‰ć≠*4ªß…thŠØ3ôs訫

Try it online or verify a few more test cases at once.

Explanation:

Mostly ported from a sub-section of this answer of mine, except with additional leading т%.

т%                     # Take modulo-100 on the (implicit) input-integer
  T‰                   # Take the divmod-10 ([n//10,n%10]) on that
    ć                  # Extract the head: n//10
     ≠                 # Check that it's NOT equal to 1 (0 if 1; 1 otherwise)
      *                # Multiply the [n%10] by this
       4ª              # Append a 4 to this list: [n%10*(n//10!=1),4]
         ß             # Pop and push the smallest number in this list
          …thŠØ        # Push dictionary string "th standards"
               3ô      # Split it into parts of size 3: ["th ","sta","nda","rds"]
                 sè    # Index the calculated minimum into this list
                       # (modular 0-based, so both 0 and 4 will index into "th ")
                   ¨   # Remove the last character
                    «  # And append it to the (implicit) input-integer
                       # (after which the result is output implicitly)

See this 05AB1E tip of mine (section How to use the dictionary?) to understand why …thŠØ is "th standards".

Haskell, 95 100 chars

h=foldr g"th".show
g '1'"th"="1st"
g '2'"th"="2nd"
g '3'"th"="3rd"
g '1'[x,_,_]='1':x:"th"
g a s=a:s

Testing:

*Main> map h [1..40]
["1st","2nd","3rd","4th","5th","6th","7th","8th","9th","10th","11th","12th","13t
h","14th","15th","16th","17th","18th","19th","20th","21st","22nd","23rd","24th",
"25th","26th","27th","28th","29th","30th","31st","32nd","33rd","34th","35th","36
th","37th","38th","39th","40th"]

Must be loaded with -XNoMonomorphismRestriction.

Javascript, 75 60

With the new arrow notation:

o=(s)=>s+((0|s/10%10)==1?"th":[,"st","nd","rd"][s%10]||"th")

Old version, without arrow notation (75 chars):

function o(s){return s+((0|s/10%10)==1?"th":[,"st","nd","rd"][s%10]||"th")}

APL (Dyalog Unicode), 38 36 bytes

Thanks to ngn for fixing a bug while maintaining byte count.

Anonymous tacit prefix function. Requires ⎕IO (Index Origin) set to 0, which is default on many systems. Even works for 0!

⍕,{2↑'thstndrd'↓⍨2×⊃⍵⌽∊1 0 8\⊂10↑⍳4}

Try it online!

{} anonymous lambda; is argument:

⍳4 first four ɩndices; [0,1,2,3]

10↑ take first ten elements from that, padding with zeros: [0,1,2,3,0,0,0,0,0,0]

 enclose to treat as single element; [[0,1,2,3,0,0,0,0,0,0]]

1 0 8\ expand to one copy, a prototypical copy (all-zero), eight copies;
  [[0,1,2,3,0,0,0,0,0,0],
   [0,0,0,0,0,0,0,0,0,0],
   [0,1,2,3,0,0,0,0,0,0],
   [0,1,2,3,0,0,0,0,0,0],
   ⋮ (5 more)
   [0,1,2,3,0,0,0,0,0,0]]

ϵnlist (flatten);
  [0,1,2,3,0,0,0,0,0,0,
   0,0,0,0,0,0,0,0,0,0,
   0,1,2,3,0,0,0,0,0,0,
   0,1,2,3,0,0,0,0,0,0,
   ⋮ (50 more)
   0,1,2,3,0,0,0,0,0,0]

⍵⌽ cyclically rotate left as many steps as indicated by the argument

 pick the first number (i.e. the argument-mod-100'th number)

 multiply two by that (gives 0, 2, 4, or 6)

'thstndrd'↓⍨drop that many characters from this string

2↑ take the first two of the remaining characters

⍕, concatenate the stringified argument to that

R, 79 76 bytes

Since there is no R solution yet... no tricks here, basic vector indexing, golfed down 3 chars thanks to Giuseppe. Previously tried index: [1+(x%%10)-(x%%100==11)] and [1+(x%%10)*(x%%100!=11)].

function(x)paste0(x,c("th","st","nd","rd",rep("th",6))[1+x%%10*!x%%100==11])

Try it online!

With substr, 79 bytes:

function(x,y=1+2*min(x%%10-(x%%100==11),4))paste0(x,substr("thstndrdth",y,y+1))

Try it online!

Julia 0.6, 68 bytes

f(x,m=x%10,p=x%100-m)="$x$(["th","st","nd","rd"][m>3||p==10?1:m+1])"

Try it online!

Straightforward remainder rules in Julia.

JavaScript (Node.js), 51 bytes

credit to @KevinCruijssen for improving the answer

n=>n+=[,'st','nd','rd'][~~(n/10%10)-1?n%10:0]||'th'

Try it online!


Explanation :

n =>                      // input
    n+=[,'st','nd','rd']     // the array of suffices add everything to n
        [~~(n/10%10)-1?n%10:0] // the full magic 
    || 'th'               // works for everything except 1 , 2 , 3 just like what you want

Procedural Footnote Language, 39 bytes

[1]
[PFL1.0]
[1] [ORD:[INPUT]]
[PFLEND]

The BODY section of the document contains a reference to footnote [1]; footnote [1] contains the [ORD]inal representation of the [INPUT].

JavaScript, 64 characters (ES3) or 47 characters (ES6)

ES3 (64 characters):

function(n){return n+=[,'st','nd','rd'][n%100>>3^1&&n%10]||'th'}

ES6 (47 characters):

n=>n+=[,'st','nd','rd'][n%100>>3^1&&n%10]||'th'

Explanation

The expression n % 100 >> 3 ^ 1 evaluates to 0 for any positive n ending with digits 0815. Thus, for any n mod 100 ending in 11, 12, or 13, the array lookup returns undefined, leading to a suffix of th.

For any positive n ending in other digits than 0815, the expression n % 100 >> 3 ^ 1 evaluates to a positive integer, invoking the expression n % 10 for array lookup, returning st,nd, or rd for n which ends with 1, 2, or 3. Otherwise, th.

Pyth, 33 bytes

+Q@.>+c"stndrd"2*]"th"7 1?qh`Q\1Z

Try it online!

Common Lisp, 63

(format t"~a~a"i(let((x(format()"~:r"i)))(subseq x(-(length x)2))))

Input is i. There's probably a more clever way to golf this.

Java 7, 91 81 bytes

String d(int n){return n+(n/10%10==1|(n%=10)<1|n>3?"th":n<2?"st":n<3?"nd":"rd");}

Port from @adrianmp's C# answer.

Old answer (91 bytes):

String c(int n){return n+((n%=100)>10&n<14?"th":(n%=10)==1?"st":n==2?"nd":n==3?"rd":"th");}

Ungolfed & test code:

Try it here.

class M{
  static String c(int n){
    return n + ((n%=100) > 10 & n < 14
                ?"th"
                : (n%=10) == 1
                   ? "st"
                   : n == 2
                      ? "nd"
                      : n == 3
                         ? "rd"
                         :"th");
  }

  static String d(int n){
    return n + (n/10%10 == 1 | (n%=10) < 1 | n > 3
                 ? "th"
                 : n < 2
                    ? "st"
                    : n < 3
                       ? "nd"
                       :"rd");
  }

  public static void main(String[] a){
    for(int i = 1; i < 201; i++){
      System.out.print(c(i) + ", ");
    }
    System.out.println();
    for(int i = 1; i < 201; i++){
      System.out.print(d(i) + ", ");
    }
  }
}

Output:

1st, 2nd, 3rd, 4th, 5th, 6th, 7th, 8th, 9th, 10th, 11th, 12th, 13th, 14th, 15th, 16th, 17th, 18th, 19th, 20th, 21st, 22nd, 23rd, 24th, 25th, 26th, 27th, 28th, 29th, 30th, 31st, 32nd, 33rd, 34th, 35th, 36th, 37th, 38th, 39th, 40th, 41st, 42nd, 43rd, 44th, 45th, 46th, 47th, 48th, 49th, 50th, 51st, 52nd, 53rd, 54th, 55th, 56th, 57th, 58th, 59th, 60th, 61st, 62nd, 63rd, 64th, 65th, 66th, 67th, 68th, 69th, 70th, 71st, 72nd, 73rd, 74th, 75th, 76th, 77th, 78th, 79th, 80th, 81st, 82nd, 83rd, 84th, 85th, 86th, 87th, 88th, 89th, 90th, 91st, 92nd, 93rd, 94th, 95th, 96th, 97th, 98th, 99th, 100th, 101st, 102nd, 103rd, 104th, 105th, 106th, 107th, 108th, 109th, 110th, 111th, 112th, 113th, 114th, 115th, 116th, 117th, 118th, 119th, 120th, 121st, 122nd, 123rd, 124th, 125th, 126th, 127th, 128th, 129th, 130th, 131st, 132nd, 133rd, 134th, 135th, 136th, 137th, 138th, 139th, 140th, 141st, 142nd, 143rd, 144th, 145th, 146th, 147th, 148th, 149th, 150th, 151st, 152nd, 153rd, 154th, 155th, 156th, 157th, 158th, 159th, 160th, 161st, 162nd, 163rd, 164th, 165th, 166th, 167th, 168th, 169th, 170th, 171st, 172nd, 173rd, 174th, 175th, 176th, 177th, 178th, 179th, 180th, 181st, 182nd, 183rd, 184th, 185th, 186th, 187th, 188th, 189th, 190th, 191st, 192nd, 193rd, 194th, 195th, 196th, 197th, 198th, 199th, 200th, 
1st, 2nd, 3rd, 4th, 5th, 6th, 7th, 8th, 9th, 10th, 11th, 12th, 13th, 14th, 15th, 16th, 17th, 18th, 19th, 20th, 21st, 22nd, 23rd, 24th, 25th, 26th, 27th, 28th, 29th, 30th, 31st, 32nd, 33rd, 34th, 35th, 36th, 37th, 38th, 39th, 40th, 41st, 42nd, 43rd, 44th, 45th, 46th, 47th, 48th, 49th, 50th, 51st, 52nd, 53rd, 54th, 55th, 56th, 57th, 58th, 59th, 60th, 61st, 62nd, 63rd, 64th, 65th, 66th, 67th, 68th, 69th, 70th, 71st, 72nd, 73rd, 74th, 75th, 76th, 77th, 78th, 79th, 80th, 81st, 82nd, 83rd, 84th, 85th, 86th, 87th, 88th, 89th, 90th, 91st, 92nd, 93rd, 94th, 95th, 96th, 97th, 98th, 99th, 100th, 101st, 102nd, 103rd, 104th, 105th, 106th, 107th, 108th, 109th, 110th, 111th, 112th, 113th, 114th, 115th, 116th, 117th, 118th, 119th, 120th, 121st, 122nd, 123rd, 124th, 125th, 126th, 127th, 128th, 129th, 130th, 131st, 132nd, 133rd, 134th, 135th, 136th, 137th, 138th, 139th, 140th, 141st, 142nd, 143rd, 144th, 145th, 146th, 147th, 148th, 149th, 150th, 151st, 152nd, 153rd, 154th, 155th, 156th, 157th, 158th, 159th, 160th, 161st, 162nd, 163rd, 164th, 165th, 166th, 167th, 168th, 169th, 170th, 171st, 172nd, 173rd, 174th, 175th, 176th, 177th, 178th, 179th, 180th, 181st, 182nd, 183rd, 184th, 185th, 186th, 187th, 188th, 189th, 190th, 191st, 192nd, 193rd, 194th, 195th, 196th, 197th, 198th, 199th, 200th, 

Mathematica 29 + 5 = 34 bytes

SpokenStringDump`SpeakOrdinal

+5 bytes because Speak function must be called before using this built-in.

Usage

SpokenStringDump`SpeakOrdinal[1]

"1st "

SpokenStringDump`SpeakOrdinal[4707]

"4,707th "

Scala 2.11.7, 151 Chars

def o(x:Int*)=x map{
case a if(a%10)==1&&a%100!=11=>a+"st"
case b if(b%10)==2&&b%100!=12=>b+"nd"
case c if(c%10)==3&&c%100!=13=>c+"rd"
case e=>e+"th"
}

Usage

   o(1,2,3,4) -> ArrayBuffer("1st", "2nd", "3rd", "4th")

C#, 62 bytes

n=>n+(n/10%10==1||(n%=10)<1||n>3?"th":n<2?"st":n<3?"nd":"rd");

Full program and verification:

using System;

namespace OutputOrdinalNumbers
{
    class Program
    {
        static void Main(string[] args)
        {
            Func<int,string>f= n=>n+(n/10%10==1||(n%=10)<1||n>3?"th":n<2?"st":n<3?"nd":"rd");
    
            for (int i=1; i<=124; i++)
                Console.WriteLine(f(i));
        }
    }
}

Python, 88 84 bytes

lambda x:x+((('th','st','nd','rd')+('th',)*6)[int(x[-1])]if('0'+x)[-2]!='1'else'th')

Ungolfed:

def y(x):
    return x + (('th', 'st', 'nd', 'rd') + ('th', ) * 6)[int(x[-1])] if ('0' + x)[-2] != '1' else 'th')

lambda x defines an anonymous function with parameter x. ((('th','st','nd','rd')+('th',)*6)[int(x[-1])] defines a tuple of the endings for numbers less than 10, the 0-th element is for 0, and so forth. the if ('0'+x)[-2] != '1' checks if there is 11, 12, or a 13 to fix, and adds then else 'th' adds th instead of st, rd, or nd.

Haxe, 83 chars

function o(n:Int){return ['th','st','nd','rd'][n%100>20||n%100<4?n%10>3?0:n%10:0];}

Based on the javascript version, fixed for n > 100 as well.

Javascript ES6, 52 chars

n=>n+(!/1.$/.test(--n)&&'snr'[n%=10]+'tdd'[n]||'th')

Oracle SQL 11.2, 101 bytes

SELECT:1||DECODE(ROUND(MOD(:1,100),-1),10,'th',DECODE(MOD(:1,10),1,'st',2,'nd',3,'rd','th'))FROM DUAL

PHP, 98 bytes

function c($n){$c=$n%100;$s=['','st','nd','rd'];echo$c>9&&$c<14||!$s[$c%10]?$n.'th':$n.$s[$c%10];}

The 11-13 bit is killing me here. Works for any integer $n >= 0.

For any integer $n:

PHP, 103 bytes

function c($n){$c=abs($n%100);$s=['','st','nd','rd'];echo$c>9&&$c<14||!$s[$c%10]?$n.'th':$n.$s[$c%10];}

Mathematica 39 45 bytes

Note: In recent versions of Mathematica, asking for the nth part of p, where p is undefined, generates an error message, but returns the correct answer anyway. I've added Quiet to prevent the error message from printing.

Quiet@StringSplit[SpokenString[p[[#]]]][[2]]&

Usage

Quiet@StringSplit[SpokenString[p[[#]]]][[2]] &[31]

31st

Quiet@StringSplit[SpokenString[p[[#]]]][[2]] &/@Range[21]

{"1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th", "10th", "11th", "12th", "13th", "14th", "15th", "16th", "17th", "18th", "19th", "20th", "21st"}


How it works

SpokenString writes out an any valid Mathematica expression as it might be spoken. Below are two examples from the documentation for SpokenString,

SpokenString[Sqrt[x/(y + z)]]

"square root of the quantity x over the quantity y plus z" *)

SpokenString[Graphics3D[Sphere[{{0, 0, 0}, {1, 1, 1}}]], "DetailedGraphics" -> True]

"a three-dimensional graphic consisting of unit spheres centered at 0, 0, 0 and 1, 1, 1"


Now, for the example at hand,

Quiet@SpokenString[p[[#]]] &[31]

"the 31st element of p"

Let's represent the above string as a list of words:

StringSplit[%]

{"the", "31st", "element", "of", "p"}

and take the second element...

%[[2]]

31st

Javascript, 75

function s(i){return i+'thstndrd'.substr(~~(i/10%10)-1&&i%10<4?i%10*2:0,2)}

C - 95 83 characters

main(n,k){scanf("%d",&n);k=(n+9)%10;printf("%d%s\n",n,k<3?"st\0nd\0rd"+3*k:"th");}

Degolfed:

main(n,k)
{
    scanf("%d",&n);
    k=(n+9)%10; // xx1->0, xx2->1, xx3->2
    printf("%d%s\n",n,k<3?"st\0nd\0rd"+3*k:"th");
}

We could do k=(n-1)%10 instead of adding 9, but for n=0 we would get an incorrect behaviour, because in C (-1)%10 evaluates to -1, not 9.

K - 44 char

It so happens that this is exactly as long as the J, and works in almost the same way.

{x,$`th`st`nd`rd@{y*(y<4)*~1=x}.-2#0,.:'x$:}

Explained:

Usage:

  {x,$`th`st`nd`rd@{y*(y<4)*~1=x}.-2#0,.:'x$:} 3021
"3021st"
  {x,$`th`st`nd`rd@{y*(y<4)*~1=x}.-2#0,.:'x$:}' 1 2 3 4 11 12 13 14  /for each in list
("1st"
 "2nd"
 "3rd"
 "4th"
 "11th"
 "12th"
 "13th"
 "14th")

Javascript, 68 71

function o(n){return n+([,'st','nd','rd'][~~(n/10%10)-1?n%10:0]||'th')}

Joint effort with ItsCosmo.

EDIT: Wasn't working properly with numbers > 100

OCaml

I'm pretty new to OCaml, but this is the shortest i could get.

let n x =
   let v = x mod 100 and string_v = string_of_int x in
   let z = v mod 10 in
   if v=11 || v=12 || v=13 then string_v^"th" 
   else if v = 1 || z = 1 then string_v^"st" else if v = 2 || z = 2 then string_v^"nd" else if v = 3 || z = 3 then string_v^"rd" else string_v^"th";;

I created a function n that takes a number as a parameter and does the work. Its long but thought it'd be great to have a functional example.

C: 95 characters

A ridiculously long solution:

n;main(){scanf("%d",&n);printf("%d%s",n,n/10%10-1&&(n=n%10)<4&&n?n>2?"rd":n<2?"st":"nd":"th");}

It needs to be mangled more.

Scala 86

def x(n:Int)=n+{if(n%100/10==1)"th"else(("thstndrd"+"th"*6).sliding(2,2).toSeq(n%10))}

Scala 102:

def x(n:Int)=if((n%100)/10==1)n+"th"else if(n%10<4)n+("thstndrd".take(n+1)%5*2.drop(n%5*2))else n+"th"

102 as well:

def x(n:Int)=if((n%100)/10==1)n+"th"else if(n%10<4)n+("thstndrd".sliding(2,2).toSeq(n%10))else n+"th"

ungolfed:

def x (n: Int) =
  n + { if (((n % 100) / 10) == 1) "th" 
        else (("thstndrd"  + ("th"  * 6)).sliding (2, 2).toSeq (n % 10))
      }

PHP, 151

I know that this program is not comparable to the others. Just felt like giving a solution.

<?$s=explode(' ',trim(fgets(STDIN)));foreach($s as$n){echo$n;echo(int)(($n%100)/10)==1?'th':($n%10==1?'st':($n%10==2?'nd':($n%10==3?'rd':'th')))."\n";}

Golfscript, 34 characters

~.10/10%1=!1$10%*.4<*'thstndrd'2/=

PowerShell, 92

process{"$_$(switch -r($_){"(?<!1)1$"{'st'}"(?<!1)2$"{'nd'}"(?<!1)3$"{'rd'}default{'th'}})"}

Works with one number per line of input. Input is given through the pipeline. Making it work for only a single number doesn't reduce the size.

Python 2.7, 137 chars

def b(n):
 for e,o in[[i,'th']for i in['11','12','13']+list('4567890')]+[['2','nd'],['1','st'],['3','rd']]:
  if n.endswith(e):return n+o

n should be a string

I know I'm beaten by the competition here already, but I thought I'd provide my idea anyways

this just basically generates a list of key, value pairs with number (as a string) ending e and the ordinal o. It attempts to match 'th' first (hence why I didn't use a dictionary), so that it won't accidentally return 'st', for example, when it should be 'th'. This will work for any positive integer

Python, 68 characters

i=input()
k=i%10
print"%d%s"%(i,"tsnrhtdd"[(i/10%10!=1)*(k<4)*k::4])

Perl, 37 + 1 characters

s/1?\d\b/$&.((0,st,nd,rd)[$&]||th)/eg

This is a regexp substitution that appends the appropriate ordinal suffix to any numbers in $_ that are not already followed by a letter. To apply it to file input, use the p command line switch, like this:

perl -pe 's/1?\d\b/$&.((0,st,nd,rd)[$&]||th)/eg'

This is a complete Perl program that reads input from stdin and writes the processed output to stdout. The actual code is 37 chars long, but the p switch counts as one extra character.

Sample input:

This is the 1 line of the sample input...
...and this is the 2.
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
101 102 103 104 105 106 107 108 109 110

Output:

This is the 1st line of the sample input...
...and this is the 2nd.
1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th
11th 12th 13th 14th 15th 16th 17th 18th 19th 20th
21st 22nd 23rd 24th 25th 26th 27th 28th 29th 30th
101st 102nd 103rd 104th 105th 106th 107th 108th 109th 110th

Numbers already followed by letters will be ignored, so feeding the output again through the filter won't change it. Spaces, commas and periods between numbers are not treated specially, so they're assumed to separate numbers like any other punctuation. Thus, e.g. 3.14159 becomes 3rd.14159th.

How does it work?


Edit: If the input is restricted to a single integer, then this 35 character solution will suffice:

s/1?\d$/$&.((0,st,nd,rd)[$&]||th)/e

Ruby, 60

It's not as good as the Perl entry, but I figured I'd work on my Ruby skills.

def o(n)n.to_s+%w{th st nd rd}[n/10%10==1||n%10>3?0:n%10]end

Function takes one integer argument, n, and returns a string as the ordinal form.

Works according to the following logic:
If the tens digit is a 1 or the ones digit is greater than 3 use the suffix 'th'; otherwise find the suffix from the array ['th', 'st', 'nd', 'rd'] using the final digit as the index.