substract all 1 number

Hello,this is what I have so far but I don't get how to do the substract the largest all 1 number you can. here is the question
*********************
Write a function that takes an unsigned
short integer and returns an signed short
number in reverse. For instance 106
reversed would be 601. Another would be
30000 gives 3. Subtract the largest all 1
digit number you can. Check to make sure the
conversion falls within the range for an
signed short. If it doesn't then output
no conversion possible else output the result.
**********************

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
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <cmath>
#include <sstream>
#include <string>

using namespace std;

signed short getReversed(unsigned short num){
   int maxValue = pow(2, (sizeof(signed short) * 8 - 1)) - 1;
   string str, temp = "";
   stringstream ss;
   ss << num;
   ss >> str;
   for(int i = 0; i < str.size(); ++i){
       temp = str[i] + temp;
   }
   ss.clear();
   ss << temp;
   ss >> num;
   if(num > maxValue){
       return -1;
   }
   else{
       return num;
   }
}

int main(){
   unsigned short num;
   cout << "Enter an unsigned value: ";
   cin >> num;
   signed short rev = getReversed(num);
   if(rev == -1){
       cout << "no conversion possible." << endl;
   }
   else{
       cout << "Reversed value is " << rev << endl;
   }
   return 0;
}
> Subtract the largest all 1 digit number you can.
I don't understand what is asking, provide some examples.
so for example if you type 3000 you reverse it and get 0003 and the smallest all 1 number you can substract would be 1 so 3-1=2 and 2 will be the answer

601 reverse is 106 so 11 is the all 1 number so 106-11=95 so 95 is the answer
you may see 111 as 1+10+100, so keep substracting until the number is too small.
By instance, with 106 you'll do
106 - 1 = 105
105 - 10 = 95
95 - 100 = (negative, return previous answer)

That can be easily done with a loop for(int K=1; /*the substraction would be nevative*/; K*=10)
Last edited on
Topic archived. No new replies allowed.