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

14th Nov 2021, 9:32 AM
raunak j
raunak j - avatar
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.
14th Nov 2021, 9:48 AM
A͢J
A͢J - avatar
+ 2
raunak j Here you can see the difference. Just try to enter 10000 https://code.sololearn.com/c028X8jjREUY/?ref=app
14th Nov 2021, 10:06 AM
A͢J
A͢J - avatar
+ 1
Ok
14th Nov 2021, 10:08 AM
raunak j
raunak j - avatar