+ 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
3 Antworten
+ 2
Here's a couple of examples
https://code.sololearn.com/cQXWfeJ3AdKY/?ref=app
https://code.sololearn.com/cUR7HTKtcTcx/?ref=app
+ 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
+ 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