Decimal to Binary

Write a program to convert decimal to binary.


import java.util.Scanner;
class Decimal2Binary
{
    public static void main()
    {
        int last;
        long binary=0;
        System.out.println("Enter a number in base 10");
        Scanner in = new Scanner(System.in);
        int a = in.nextInt();
        for(int i=0;a>0;i++)
        {
            last= a%2; a=a/2;
            binary=(long)Math.pow(10,i)*last + binary;
        }
        System.out.println("Given number in base 2   "+binary);
    }
}

For any desired program, please comment below. We will try our best to make that program available.

Comments

Popular Posts