# Convert Decimal To Binary

• 08-04-2010, 07:26 AM
aspire007
Convert Decimal To Binary
i have the code:

import java.io.*;

public class Convert{
public static void main(String args[]) throws IOException {

int a[] = {0, 1};

int number;
int r;

System.out.println();
System.out.print("Enter Decimal Number: ");
System.out.println();

do{

r=number%2;

if(r > 0){
System.out.println(a[1]); //prints the binary 1
}

else{
System.out.println(a[0]); //prints the binary 0
}

number=number / 2;
}while(number>0);

System.out.println();
}
}

and the output is:
--------------------------------
Enter Decimal Number: 10

0
1
0
1

--------------------------------

Please help me on how to make the output like below using the following codes above. Thanks for those who help me...!
------------------------
Enter Decimal Number: 10

Binary : 1010
------------------------
• 08-04-2010, 08:28 AM
Zack
Think about this: you have four strings (0, 1, 0, 1). If you were to store these in a variable, you could have: "0101". Then you could reverse it to get "1010".

Your method may not be the best but it appears to work for you--so what I'm suggesting is that instead of using println() for each number you print, create an empty string that you prepend each time you have another number. So it would be "0", "10", "010", "1010".

Much luck! :)
• 08-04-2010, 03:06 PM
Norm
Do you have to print the binary digits as you compute them or can you save them and print them all after they are all available?
If you can save them, use String concatenation to save them them into a String and then print that String when done.
• 08-04-2010, 03:26 PM
j2me64
if you are only interested in the string-result, and not each single bit, you can also replace your do-while-loop with the following code

Code:

```                System.out.println("Binary: "                                 + Long.toBinaryString(Long.parseLong(String.valueOf(number))));```
• 08-04-2010, 04:18 PM
Norm
Calling someone else's methods doesn't demonstrate that the student knows how to solve the problem.
• 08-05-2010, 03:43 AM
aspire007
I think it is the shortcut method where in it automatically converts the decimal to binary to its correct output position. Our instructor said that we do not use the shortcut method.."
• 08-05-2010, 08:12 AM
Zack
Unfortunately the method you are using can only work this way (in the reverse direction). So you have two choices:

1) Use String prepending (concatenation to the start of the string), then print the string.
2) Use a different method (I can show you a different one).
• 08-06-2010, 08:07 AM
aspire007
Zack can you show me the different one method you said? Can you help me cause its an assignment, i am only newbie and i want to learn...i've tried searching about the string prepending or the concatenation you said but i dont have found sample..i'm running out of time for the submission to our instructor..hope you can help me..."
• 08-06-2010, 08:32 AM
Zack
Have you done string concatenation? Like...
Code:

`String x = "a" + "b";`
Because if you have, then you can use this for the assignment.

Alternately, here is another type of code you can use:
How to Convert from Decimal to Binary - wikiHow
(The second method is what you used; the first method is what I'm suggesting.)