Binary converter | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Binary converter

it does not work why https://code.sololearn.com/c4CbZEi2Ty1g/?ref=app

1st Apr 2021, 7:23 PM
Ouardia Oughris
Ouardia Oughris - avatar
6 Answers
+ 1
it works thank you so much
1st Apr 2021, 7:37 PM
Ouardia Oughris
Ouardia Oughris - avatar
+ 1
Martin Taylor thank you so the calculation method is not necessary
1st Apr 2021, 8:23 PM
Ouardia Oughris
Ouardia Oughris - avatar
+ 1
import java.util.Scanner; //your code goes here public class Converter { public static String toBinary(int num){ String binary=""; while(num > 0) { binary = (num%2)+binary; num /= 2; } return binary; } } public class Program { public static void main(String[ ] args) { Scanner sc = new Scanner(System.in); int x = sc.nextInt(); System.out.print(Converter.toBinary(x)); } }
16th Jun 2021, 10:47 AM
Dharmi Sri
Dharmi Sri - avatar
0
You are return value of num which is 0 at end. But you need to return binary which is calculated.. so use. return binary; Instead of return Integer.toBinaryString(num);
1st Apr 2021, 7:32 PM
Jayakrishna 🇮🇳
0
thank you
1st Apr 2021, 7:35 PM
Ouardia Oughris
Ouardia Oughris - avatar
0
You're welcome..
1st Apr 2021, 7:42 PM
Jayakrishna 🇮🇳