Created
February 1, 2017 16:15
-
-
Save panwarab/8d72507ec6693cd134d9013a1769cace to your computer and use it in GitHub Desktop.
Program converting a given number from decimal to binary
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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