Hey everyone.
I'm trying to write a program which converts decimal into binary using recursion and vice versa.
So far i have been able to write the method for decimal to binary and it compiles fine and runs ok, apart from the fact that it displays the binary answer backwards.
Here is my code:
For example, if i put in 5 to convert to binary, instead of getting 0101 as the answer i get 1010.
How can i reverse the output? I have been trying so hard...
Thanks
I'm trying to write a program which converts decimal into binary using recursion and vice versa.
So far i have been able to write the method for decimal to binary and it compiles fine and runs ok, apart from the fact that it displays the binary answer backwards.
Here is my code:
Code:
public static String decTobin (int n){
if (n==0){
return "0";
}
if (n%2==0){
return "0" + decTobin(n/2);
}
else {
return "1" + decTobin(n/2);
}
}
For example, if i put in 5 to convert to binary, instead of getting 0101 as the answer i get 1010.
How can i reverse the output? I have been trying so hard...
Thanks