0
What is wrong in my code for the int to binary assignment?
Whatever change I make, I still face some errors. Can you please tell me what is my mistake in this code? 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 num; } } 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)); } }
5 odpowiedzi
+ 1
You put a ';' where it's supposed to be a '{' at the end of
public static String toBinary(int num)
+ 1
Try return binary
0
Igor Kostrikin I tried it but it still gives "illegal start of type" error both for the line "while(num>0)" and line "return num" or as you recommend "return binary".
0
Thank you so much Odyel and Igor Kostrikin the corrections of both of you were required :D