+ 1

I'm trying to create calculator without binary function in java

Tell me how can I create Decimal to Binary Converter Without using binary function See my code and tell me what I can do ? https://code.sololearn.com/cOEgRoFs46i5/?ref=app

15th Sep 2021, 7:02 PM
Abhishek Shelar
Abhishek Shelar - avatar
3 Antworten
15th Sep 2021, 7:35 PM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Try it this way: int num = 5; String bin = ""; while(num > 0) { bin = String.valueOf(num%2) + bin; num /= 2; } System.out.println(bin); // 101
15th Sep 2021, 7:33 PM
Coding Cat
Coding Cat - avatar
+ 1
Thanks ChaoticDawg I had a look at your examples. I've seen that my first version has a problem with num == 0. And that I do not need a conversion like String.valueOf(). So this one is better: int num = 5; String bin = ""; do { bin = ( num % 2 ) + bin; num /= 2; } while (num > 0); System.out.println(bin); // 101
15th Sep 2021, 8:19 PM
Coding Cat
Coding Cat - avatar