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

14th Feb 2021, 7:04 PM
simin kazemi
simin kazemi - avatar
4 Answers
+ 1
You put a ';' where it's supposed to be a '{' at the end of public static String toBinary(int num)
14th Feb 2021, 7:29 PM
Odyel
Odyel - avatar
+ 1
Try return binary
14th Feb 2021, 7:18 PM
Igor Kostrikin
Igor Kostrikin - avatar
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".
14th Feb 2021, 7:26 PM
simin kazemi
simin kazemi - avatar
0
Thank you so much Odyel and Igor Kostrikin the corrections of both of you were required :D
14th Feb 2021, 7:32 PM
simin kazemi
simin kazemi - avatar