I'm having a recursion challenge. The goal is to return "n + the sum of the last digit to the left". Example:
Entry: 54321
Output: 54326 (54321 + 5)
But the only way I got it was like this:
#include <stdio.h>
int somault(int n){
if(n < 10) return n;
return somault(n/10);
}
int main() {
int n;
scanf("%d", &n);
printf("%d", n+somault(n));//A soma é realizada aqui, não dentro da função.
return 0;
}
Is it possible to return this value based on the following criteria?
- Only one function
- Recursive function
- Without using vectors, global variables and / or pointers
I tried other ways, but none returned the correct value. Just because I can not get the value of the last call and only do with the first call. I tried to use static
to count or split by increasing base 10 to get the last but did not work.