0

40 Code project

Hello Dear Coders. Can you give me some help. I solved the problem in 40 Code project by myself, but i have feeling like i did it not good. Can you please show me any other way for solving 40 Code project, my way was like that: import java.util.Scanner; public class Program { public static void main(String[] args) { Converter converter = new Converter(); Scanner sc = new Scanner(System.in); int x = sc.nextInt(); converter.setNum(x); System.out.println(converter.toBinary()); } } class Converter{ int num; String binary=""; public void setNum(int setNum){ this.num = setNum; } String toBinary(){ while (num>0){ binary = (num%2)+binary; num /= 2;} return binary; } } thx )

16th Oct 2022, 6:28 PM
Juan
1 Antwort
+ 1
//Expected code there is : import java.util.Scanner; 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)); } } class Converter{ public static String toBinary(int num){ String binary=""; while (num>0){ binary = (num%2)+binary; num /= 2; } return binary; } }
16th Oct 2022, 6:42 PM
Jayakrishna 🇮🇳