+ 1
why are we writing String in the method line (toBinary(int num))??
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; } return (binary); } //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)); }
4 odpowiedzi
+ 2
raunak j
Because when you convert a long number to binary then it will exceed the size of integer so in this case you may get exception.
+ 2
raunak j
Here you can see the difference. Just try to enter 10000
https://code.sololearn.com/c028X8jjREUY/?ref=app
+ 1
Ok