logo CodeStepByStep logo

printBinary

Language/Type: C++ recursion

Write a recursive function named printBinary that accepts an integer and prints that number's representation in binary (base 2) to the console. For example, the call of printBinary(43); should print 101011 . You do not need to print an endl character at the end of your output.

If the integer is negative, print the binary representation preceded by a minus sign. For example, the call of printBinary(-6); should print -110 .

Constraints: Do not use any loops. Do not solve this problem by converting the number into a string. Do not solve it by using the pow function or any form of computing exponents; that is inefficient and not needed to solve the problem. Do not declare any auxiliary collections such as a vector or map. Do not solve it by using the ostream manipulation functionality such as setbase.

Function: Write a C++ function as described, not a complete program.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.