# Thread: converting decimal to binary value using recursion in java

1. Member
Join Date
Jul 2009
Posts
4
Rep Power
0

## 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

2. Member
Join Date
Jul 2009
Posts
4
Rep Power
0

## 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);
}

}

3. @OP, what you mean by recursive method? Can you explain bit more clearly. What javamadd explain is straightforward, no any recursive process going on.

4. Member
Join Date
Jul 2009
Posts
6
Rep Power
0

## 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.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•