Make an algorithm to convert decimal into binary | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Make an algorithm to convert decimal into binary

can u do this

19th May 2018, 1:20 PM
sam
8 Answers
+ 2
sam 0. We import LinkedList and Scanner from the java.util package. 1. We create an instance of the scanner. 2. It takes a number input from the user. 2. It checks whether it is a number or not. 3. If it isn't, error message is printed. 4. Else, the conversion process begins. 5. We define a number "b" and assign the value of the input into it. 6. We define a boolean "c". 7. If the input is a positive number, this boolean will be true. 8. Else, it'll be false. 9. Regardless of the boolean value, b is made a positive number. 10. We create a new String LinkedList. 11. We define a number "n" and assign 64 to it. 12. We divide b by 2^n and add the result into the LinkedList, and decrease b from the remainder. 12a. n is decremented by 1. 12b. We go back to step number 12 until n reaches zero. 13. We define a new String for the output. 14. We pack the entire LinkedList value into the String. 15. We take the boolean mentioned in step 6. 16. If it is true, nothing happens. 17. If it is false, we add "-" to the beginning of the String. 18. The String is returned as a result. 19. The String is printed. I hope this helps ☺
19th May 2018, 1:38 PM
Ibaadi Jaya
Ibaadi Jaya - avatar
+ 1
can u give me the algorithm of this
19th May 2018, 1:23 PM
sam
19th May 2018, 1:36 PM
RainStorm
RainStorm - avatar
+ 1
19th May 2018, 1:43 PM
RainStorm
RainStorm - avatar
+ 1
Here is a simple algorithm: 1. Divide decimal number by 2 2. Note the remainder 3. If the quotient is 0, continue to step 4; otherwise return to step 1 with the quotient as the new decimal number. 4. The remainders sorted from last to first is your binary number -------------------------------------------------------- For example, let's take the number 10 (in decimal): 10 // 2 = 5 (rem = 0) 5 // 2 = 2 (rem = 1) 2 // 2 = 1 (rem = 0) 1 // 2 = 0 (rem = 1) ['//' means floor division, which means we remove the decimal numbers after the division] Since the quotient is zero, we can sort the remainders in the order of last to first: => 1010 Therefore 1010 is the binary representation of 10. -------------------------------------------------------- Hope this was of some help :)
19th May 2018, 2:51 PM
Just A Rather Ridiculously Long Username
+ 1
thank u guys for help me 😊😊😊
19th May 2018, 5:21 PM
sam
12th Mar 2019, 2:00 PM
RainStorm
RainStorm - avatar
0
Please do not plagiarize without crediting my name... https://code.sololearn.com/cnT9clyEeuo8/?ref=app
19th May 2018, 1:21 PM
Ibaadi Jaya
Ibaadi Jaya - avatar