Binary converter | Sololearn: Learn to code for FREE!

+1

Binary converter

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

4/1/2021 7:23:16 PM

Ouardia Oughris

7 Answers

New Answer

+1

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

+1

it works thank you so much

+1

Martin Taylor thank you so the calculation method is not necessary

+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)); } }

0

thank you

0