Clear this to me, this binary thing. And the strange if clause. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Clear this to me, this binary thing. And the strange if clause.

import java.util.Scanner; //your code goes here public class Converter { public static String toBinaryString ( int n ) { if ( n < 0 ) throw new IllegalArgumentException ( "n must be non-negative" ); // special case if ( n == 0 ) return "0" ; // build string from right to left String s = "" ; while ( n > 0 ) { if ( n % 2 == 0 ) s = '0' + s ; else s = '1' + s ; n = n / 2 ; } return s ; } } public class Program { public static void main(String[ ] args) { Scanner sc = new Scanner(System.in); int x = sc.nextInt(); System.out.print(Converter.toBinaryString(x)); } }

24th Dec 2020, 4:04 AM
Jo$y ☑️
Jo$y ☑️ - avatar
0 Answers