0

Please I need help

The binary numeric system uses only two digits: 0 and 1. Computers operate in binary, meaning they store data and perform calculations using only zeros and ones. You need to make a program to convert integer numbers to their binary representation. Create a Converter class with a static toBinary() method, which returns the binary version of its argument. The code in main takes a number as input and calls the corresponding static method. Make sure the code works as expected. Sample Input: 42 Sample Output: 101010 What I did import java.util.Scanner; public class Converter{ public static int toBinary(int x); String binary=""; while (x>0){ binary=(x%2)+binary; x=x/2; } return x; } 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 Dec 2021, 6:55 PM
Egede Catherine Faith
Egede Catherine Faith - avatar
3 Answers
+ 1
Thank you Coding Santa
14th Dec 2021, 8:04 PM
Egede Catherine Faith
Egede Catherine Faith - avatar
0
Your method returns "x" better is return "binary" But therefore you have to change the return type of your method from int to String.
14th Dec 2021, 7:09 PM
Coding Cat
Coding Cat - avatar
0
And one pair of curly braces are missing: After the method declaration (instead that semi colon) and after the return statement. public class Converter { public static String toBinary(int x) { String binary=""; while (x>0) { binary=(x%2)+binary; x=x/2; } return binary; } }
14th Dec 2021, 7:20 PM
Coding Cat
Coding Cat - avatar