Showing posts with label Decimal to Binary conversion. Show all posts
Showing posts with label Decimal to Binary conversion. Show all posts

Sunday, 11 August 2013

Program in java Decimal to Binary conversion

import java.io.*;
class DectoBin
{
    public static void main(String args[])
    {
        int i=0;
        int bin[]=new int[10];
        try
        {
            DataInputStream cin=new DataInputStream(System.in);
            System.out.println("Enter Decimal Number");
            int dec=Integer.parseInt(cin.readLine());
            while(dec>0)
            {
                bin[i]=dec%2;
                dec=dec/2;
                i++;
            }
            i--;
            for(int j=i;j>=0;j--)
            System.out.print(bin[j]);
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }
}