# Thread: Convert Decimal To Binary

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

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

2. 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! :)

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

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

Java Code:
```		System.out.println("Binary: "
+ Long.toBinaryString(Long.parseLong(String.valueOf(number))));```

5. Calling someone else's methods doesn't demonstrate that the student knows how to solve the problem.

6. Member
Join Date
Jul 2010
Posts
4
Rep Power
0
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.."

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

8. Member
Join Date
Jul 2010
Posts
4
Rep Power
0
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..."

9. Have you done string concatenation? Like...
Java 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.)

#### Posting Permissions

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