What will be the output of the below function if n is positive ?

    void myFun(unsigned int n)
    {
        if(n != 0)
        {
            myFun(n/2);
            printf("%d", n % 2);
        }
    }

Solution:

The function prints the value of n in binary notation. For example: If n = 25 then the output will be 1 1 0 0 1.

Actually printing the remainders in reverse order (Head Recursion).

One Response

Leave a Reply

Your email address will not be published. Required fields are marked *