Ask Question
31 August, 04:36

Write a method named toBinary that accepts an integer as a parameter and returns a string of that number's representation in binary. For example, the call of toBinary (42) should return "101010".

+3
Answers (1)
  1. 31 August, 04:52
    0
    using c++

    #include

    string toBinary (int number) {

    int reminder;

    string result;

    while (number==0) {

    remainder=number%2;

    number=number/2;

    result=result + (char) remainder;

    }

    result=strrev (result);

    return result;

    }

    Explanation:

    the function toBinary () takes number as parameter and return binary value of that number in string format. we are iterating while loop until the number becomes 0.% operator gives remainder and / operator gives quotient. we are storing all remainders by dividing the number with 2 and making quotient as number in each iteration. at the end we are reversing the remainders to get binary equivalent of the number and returning that.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write a method named toBinary that accepts an integer as a parameter and returns a string of that number's representation in binary. For ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers