Write a recursive method in pseudo code that returns the num

Write a recursive method in pseudo code that returns the number of 1\'s in the binary representation of N. Use the fact that this equal to the number of 1\'s in the representation of N/2, plus 1, if N is odd.

Solution

public int countones(int n)
{

if(n==0)
{
return 0;
}
else
{
return (n%2) + countones(n/10);
}

}

1: Read N
2: countone(N)
    If N=0 return 0
    If N%2 = 1 return N%2 + countone(N/10)
3:Print N
4:Stop

 Write a recursive method in pseudo code that returns the number of 1\'s in the binary representation of N. Use the fact that this equal to the number of 1\'s i

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site