Desperate Help

I have to write a recursive method that returns the number of 1s (ones)in a binary number entered by the user from the console(represented by N). I have to use the fact that this number equals the number of 1s in the representation of N/2, plus 1, if N is odd. Does anyone have any suggestions.

Thanks

Comments

  • [b][red]This message was edited by Illco at 2002-9-16 7:27:25[/red][/b][hr]
    : I have to write a recursive method that returns the number of 1s (ones)in a binary number entered by the user from the console(represented by N). I have to use the fact that this number equals the number of 1s in the representation of N/2, plus 1, if N is odd. Does anyone have any suggestions.
    :
    : Thanks
    :

    [code]
    int numOnes( int number )
    {
    if ( number == 0 )
    return 0;
    else if ( number == 1 )
    return 1;
    else
    return ( numOnes( number/2 ) + (number % 2 != 0 ? 1 : 0) );
    }
    [/code]


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion