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);
}
}
Comments
Post a Comment