In English, we can abbreviate an ordinal numeral by appending the last two letters of the word with the corresponding number. For example, “first” can be abbreviated as “1st”; “second” can be formatted as “2nd”; “third” can be formatted as “3rd”; “fourth” can be formatted as “4th”.
Write a program that given some numbers, formats the corresponding English ordinal numerals.
Each line of input contains a natural number n where 0 < n < 10 000.
For each line of input there should be a line of output with the number given on the input followed by the English ordinal suffix.
Example input
1
2
3
4
56
Example output
1st
2nd
3rd
4th
56th
suffix
and ordinal
functionsYour program should be implemented using two functions suffix
and ordinal
.
The suffix
function should return a string with just the suffix.
The ordinal
function should return a string with the abbreviated ordinal
— the number followed by the suffix.
Please refer to the information for your chosen language:
Python definitions: def suffix(n):
, def ordinal(n):
;
C prototypes: const char* suffix(int);
, char* ordinal(int, char*);
— ordinal
should write the string to the given buffer and return a pointer to it
whereas suffix
should return a pointer to a constant string;
Haskell type: suffix, ordinal :: Int -> String
;
C++ prototypes: const char* suffix(int);
, char* ordinal(int, char*);
— ordinal
should write the string to the given buffer and return a pointer to it
whereas suffix
should return a pointer to a constant string;
C# definitions: public static string Suffix(int n)
, public static string Ordinal(int n)
— available in a public class Program
Java definitions: public static String suffix(int n)
, public static String ordinal(int n)
— available in a public class Ordinal
JavaScript definitions: function suffix(n)
, function ordinal(n)
;
Lua definitions: function suffix (n)
, function ordinal (n)
;
Ruby definitions: def suffix(n)
, def ordinal(n)
.
Copyright © 2020-2023 Rudy Matela
This text is available under the CC BY-SA 4.0 license.
Originally available on cscx.org/ordinal