Reccursion
Nov 20, 2015 at 7:49pm UTC
I know how to get the 155, but how is rec(19683) printing 16633? I'm getting 19633 for some reason. Thanks for taking the time to read.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
using namespace std;
int rec (int n) {
if (n < 10) return n;
return 100 * rec(n / 100) + 11 * (n % 10);
}
int main () {
cout << rec(135) << endl; // prints 155
cout << rec(19683) << endl; // prints 16633, but how?
return 0;
}
Nov 20, 2015 at 8:31pm UTC
What does it mean that you are getting 19633? Did you do the calculation by hand? Then most probably you made a mistake... Computers might not have imagination, but they are pretty good with maths!
Nov 20, 2015 at 8:41pm UTC
Yeah I 'm doing it by hand, I want to know why I'm getting a different answer.
Nov 20, 2015 at 9:05pm UTC
As I said, check your calculation: there must be some mistake.
Topic archived. No new replies allowed.