I am trying to convert a decimal number to binary but the code I've written doesn't work in all test cases. Here's the code:
import java.util.Scanner;
public class Converter{
public static int toBinary(int num) {
String binary="";
while (num>0){
binary = (num%2)+binary;
num /=2;
}
int bin = Integer.parseInt(binary);
return bin;
}
}
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));
}
}
I would be grateful if somebody could tell me what's wrong.