Can someone please explain to me what is the difference between the two functions below?
I created the function in the top and my friend created the function in the bottom. I just want to know why the function with the while loop prints the binary numbers backwards. And the recursive function prints the binary numbers correctly.
Both functions split values from the least-significant end of your number.
That's akin to reading the number 723 as 3, 2, 7.
The iterative function doesn't wait to write the digit it gets. So as soon as it gets that 3 it writes it, making it first in the output --> "327".
The recursive function waits until after the remaining digits are handled, and only then prints the number. Hence, the numbers come out in backwards order, which are correct: 7, 2, 3 --> "723".
You can fix the iterative version by storing the results in a string. Instead of couting the digit as soon as you get it, append it to a string. Once done, print the string backwards.
You are starting with the right most digit instead of the leftmost like recursion. Basically for example with 11 the binary is 1011 which is 8 + 2 + 1 with the while loop (yours) you check if its divisible by 2, 4, 8, 16...instead of 16, 8, 4, 2...