Ask 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 example, the call of toBinary (42) should return "101010".

+2
Answers (1)
  1. 12 April, 20:41
    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 & 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