I can't spot my mistake, I've also done the problem on paper and it should return the right result, instead it's giving me 6. Could you help me?
Code:
/* Write a function that given a natural number computes the value resulting from the digits of its decimal representation when interpreted as a hexadecimal number.
Example: f(312) = 3 * 256 + 1 * 16 + 2 = 786
Hint: use the same recursive decomposition, but when reconstructing the number, use the new base (16).
*/
#include <stdio.h>
#include <math.h>
int hex(int n, int i){
i=0;
if(n==0)
return 0;
else
return (n%10)*pow(16,i)+hex(n/10,i+1);
}
int main()
{
int a=312;
int i=0;
printf("The number %d in base 16 is %d\n", a, hex(a,i));
return 0;
}