# converting decimal to binary value using recursion in java

• 07-24-2009, 11:25 AM
Anindo
converting decimal to binary value using recursion in java
Hello
i am trying to make a recursive method that take a decimal number and convert it into its binary number. i think there will be two base cases that remainder is one or zero .can anyone help me further with simple solution
• 07-24-2009, 10:13 PM
Simpler way to do the same
import java.util.Scanner;

public class toBinary {

/**
*/
public static void main(String[] args) {
int num;
System.out.println("enter the decimal number");
Scanner scanboy = new Scanner(System.in);
num = scanboy.nextInt();
System.out.print("The decimal number is " + num);
String binary = Integer.toBinaryString(num);
System.out.print(" And the corresponding Binary is ");
System.out.print( ""+ binary);
}

}
• 07-25-2009, 04:28 AM
Eranga
@OP, what you mean by recursive method? Can you explain bit more clearly. What javamadd explain is straightforward, no any recursive process going on.
• 07-25-2009, 02:44 PM
Hardik
Reccursion method to convert decimal to binary.
hey here is the code :

class Binary
{
int a1,b1;
void convert(int a1)
{
b1 = a1 % 2;
if(a1 > 0)
{
convert(a1/2);
System.out.println("binary bits are = "+b1); //i think the result would you get reverse of the byte so u take care about it
}
}
}
class DECTOBIN
{
public static void main(String args[])
{
Binary obj = new Binary();
obj.a1 = Integer.parseInt(args[0]);
obj.convert(obj.a1);
}
}

Ok well. this is the code. But i didnt try it.