Write a method named decimalToBinary that accepts an integer as a parameter and returns an integer whose digits look like that number's representation in binary (base-2). For example, the call of decimalToBinary(43) should return 101011. Constraints: Do not use a string in your solution. Also do not use any built-in base conversion methods like Integer.toString .

Respuesta :

Answer:

In Java:

public static int decimalToBinary(int decimal){

int binary = 0; // initial value

int currUnitPlace = 1; // working location

while(decimal > 0) {

// put remainder in current unit place

binary += currUnitPlace * (decimal%2);

decimal /= 2; // move to next bit

currUnitPlace *= 10; // move unit place

}

return binary;

}

ACCESS MORE