Hello, I was trying the binary to decimal conversion with recursion and the diff between that and the octal to decimal one too and the only difference was 1 number (8 or 2)
Literally changing line 4 to: return n%10 + (8*btod(n/10));
Makes the diff, tracing it gets me the results but the reason behind it is.. ? Is it cause 2^0, 2^1,... for binary and 8^0, 8^1,... for octal ? I mean that could be the reason but I still don't know why we multiply either of them by the function call.
sum(digitn * basen) where n = 0 ... number size - 1
What you have to do is separating the digit (n%10) and multiplying the base with it self as many times as its position within the number and multiplying this with the digit.
long binaryToDecimal(long n) {
int remainder;
long decimal = 0, i=0;
while(n != 0) {
remainder = n%10;
n = n/10;
decimal = decimal + (remainder*pow(2,i));
++i;
}
return decimal;
}
between this and the recursive function, where is this part: (remainder*pow(2,i)); cause idk if there is anything equivalent to squaring but multiplying by the number x times, but maybe I am not aware of a special thing here