Thursday, September 10, 2015

Convert Decimal To Binary



import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class DecimalToBinary {

public static void main(String[] args) {

Scanner scn=new Scanner(System.in);
System.out.println(" type a no. to convert into binary");
int n=scn.nextInt();
binary(n);
}

public static void binary(int n)
{
List<Integer> ls=new ArrayList();
while(n>0)
{
if(n%2==0)
{
n=n/2;
ls.add(0);
}
else if (n%2==1)
{
n=n/2;
ls.add(1);
}
else
{
ls.add(1);
}
}
Collections.reverse(ls);
System.out.println(ls);
}
}