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

List of all available Websheets


Viewing cpp/control/revdigits by redekopp@usc.edu. 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-zero integer input (which could be negative) and
outputs the string of digits of the number in reverse order.  If the number
is negative you should still output a negative sign first.
Public permissions remove
Remarks
Comments, history, license, etc.
 
Originally by Mark Redekopp (redekopp@usc.edu) and Dave Pritchard (daveagp@gmail.com)
remove
Engine
Template / Reference solution
 
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
   int n;
   // write code that allows the user to enter a non-zero integer 
   // which could be either (pos. or neg). and then 
   // outputs the number with digits reversed
   //  Example 1:    321    outputs:  123
   //  Example 2: -14539    outputs:  -93541
   // Output a newline after you output the digits in reverse.
   cout << "Enter an integer: " << endl;
   cin >> n;
\[      
   if(n == 0) { cout << 0; }
   else {
     if(n < 0){
       cout << "-";
       n = -n;
     }
     while( n > 0 ) {
       cout << n%10;
       n = n/10;
     }
   }
   cout << endl;
]\
  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": "-14539"},
   {"stdin": "-321"},
   {"stdin": "321"}
]
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'.