Skip to content

Instantly share code, notes, and snippets.

@panwarab
Created February 1, 2017 16:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save panwarab/8d72507ec6693cd134d9013a1769cace to your computer and use it in GitHub Desktop.
Save panwarab/8d72507ec6693cd134d9013a1769cace to your computer and use it in GitHub Desktop.
Program converting a given number from decimal to binary
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
BufferedWriter writer=new BufferedWriter(new OutputStreamWriter(System.out));
int t=Integer.parseInt(reader.readLine()); //test cases
while(t-->0)
{
int num=Integer.parseInt(reader.readLine());
String res=""; // not efficient, look into StringBuffer
while(num>0)
{
res+=(num%2)+"";
num=num/2;
}
writer.write(printReverse(res)+"\n");
}
writer.close();
}
static String printReverse(String s) //made static so that no object is required for a call
{
StringBuffer sb=new StringBuffer(s);
return sb.reverse().toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment