Advertisement
karlakmkj

Sololearn - Binary Converter challenge

Aug 16th, 2021
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.61 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. //your code goes here
  4. class Converter{
  5.     public static String toBinary(int num){ //remember to include return type as String!
  6.         String binary="";
  7.        
  8.         //Loop block is given
  9.         while(num > 0) {
  10.         binary = (num%2)+binary;
  11.         num /= 2;
  12.         }
  13.         return binary; //to return the binary string in this method
  14.     }
  15. }
  16.  
  17. //This is given
  18. public class Program {
  19.     public static void main(String[ ] args) {
  20.         Scanner sc = new Scanner(System.in);
  21.         int x = sc.nextInt();
  22.         System.out.print(Converter.toBinary(x));
  23.     }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement