Ask Question
21 August, 16:45

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.

+2
Answers (1)
  1. 21 August, 17:07
    0
    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;

    }
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Write a method named decimalToBinary that accepts an integer as a parameter and returns an integer whose digits look like that number's ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers