Not logged in. Log in with GitHub. About Websheets.

List of all available Websheets


Viewing cpp/control/nth by daveagp@gmail.com. You have unsaved changes. Log in above to create, edit, preview and publish Websheets.
PropertyValue
Description
html markup
shown to student
 
This program should take a non-negative integer input <tt>n</tt>. 
It should print out the 
English <i>ordinal number</i> corresponding to <tt>n</tt>.
For example,
<ul><li>for input <tt>1</tt> it should output <tt>1st</tt>
<li>for input <tt>2</tt> it should output <tt>2nd</tt>
<li>for input <tt>3</tt> it should output <tt>3rd</tt>
<li>for input <tt>4</tt> it should output <tt>4th</tt>
</ul>
et cetera. 
<p>Hint: the last digit is important, and you can compute the last
digit using <tt>n % 10</tt>, which is the remainder when you divide <tt>n</tt>
by 10. You also need a special case when the last two digits are 11, 12, or 13.
Public permissions remove
Engine
Template / Reference solution
 
#include <iostream>
using namespace std;
int main() {
   int n;
   cin >> n;
\[
   if (n % 100 >= 11 && n % 100 <= 13)
      cout << n << "th";
   else if (n % 10 == 1)
      cout << n << "st";
   else if (n % 10 == 2)
      cout << n << "nd";
   else if (n % 10 == 3)
      cout << n << "rd";
   else
      cout << n << "th";
]\
   return 0;
}
C++ test suite
json list of stdin/args tests
e.g. [{"stdin":"hi", "args":["4", "5"]},
{"stdin":"noargs"}]

to just run once with no input use [{}]
 
[
   {"stdin": "1"},
   {"stdin": "2"},
   {"stdin": "3"},
   {"stdin": "4"},
   {"stdin": "5"},
   {"stdin": "6"},
   {"stdin": "7"},
   {"stdin": "8"},
   {"stdin": "9"},
   {"stdin": "10"},
   {"stdin": "21"},
   {"stdin": "132"},
   {"stdin": "4323"},
   {"stdin": "12984"},
   {"stdin": "11"},
   {"stdin": "212"},
   {"stdin": "90013"}
]
Solution visibility remove


Optional properties:

Note: problems are open-source by default (see 'Public permissions'). Assumed license for open problems is Creative Commons 4.0 Attribution-ShareAlike unless specified in 'Remarks'.