- 3
Binary Convertor
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
4 odpowiedzi
+ 4
show your attempt first ??
+ 1
Nomsa Constance Masuku If you have a problem
•Please give us your code or at least put a sample code in the sololearn question forum so that we can help you in your work
•If you have any doubts then ask us
•And please mention your question description in detail
•Search in question forum before posting a question
•Don’t post comments on question forum
Happy Coding!
https://www.sololearn.com/discuss/1316935/?ref=app
- 2
import java.util.Scanner;
//your code goes here
public class Converter{
static String toBinary(int num)
{
String binary="";
while(num > 0) {
binary = (num%2)+binary;
num /= 2;
}
return binary;
}
}
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));
}
}