#### Howdy, Stranger!

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

# Desperate Help

Member Posts: 1
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

• Member Posts: 382
[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]

• USAMember Posts: 0

// http://forcoder.org // free ebooks and video tutorials about [ Ruby Objective-C R PHP Visual Basic Assembly MATLAB C# Delphi C Python JavaScript Perl C++ Go Java Swift PL/SQL Visual Basic .NET Scratch D SAS Hack Julia Scala Lua F# Prolog Transact-SQL Bash ML Apex Dart Kotlin Fortran COBOL Crystal LabVIEW Erlang FoxPro VBScript Alice Lisp Ada ABAP Logo Awk Rust Clojure Scheme ]