1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Fantom [35]
3 years ago
9

Write a recursive method that returns the number of 1’s in the binary representation of N. Use the fact that this is equal to th

e number of 1’s in the representation of N/2, plus 1, if N is odd

Computers and Technology
1 answer:
madreJ [45]3 years ago
3 0

Answer:

Here is the recursive method NoOfOnes :

public static int NoOfOnes(int N)    { // method that takes number N as argument and returns the number of 1’s in the binary representation of N

       if(N==0)  { //base case 1

           return 0;        } //if the value of N is equal to 0 then method returns 0

       else if(N==1){ //base case 2

           return 1;        } // if the value of N is equal to 1 then method returns 1

       else if(N%2==1)  { //if N is odd (recursive case)

           return NoOfOnes(N/2)+1;   } //calls method recursively and divides N by 2 plus 1

       else{ //when N i even (recursive case)

           return NoOfOnes(N/2);   }   }   //calls method recursively and divides N by 2  to return number of 1's in binary representation of N

Explanation:

Here is the complete JAVA program:

import java.util.Scanner; //to take input from user

public class Main {

   public static int NoOfOnes(int N)    {//the method that takes number N as argument and returns the number of 1’s in the binary representation of N

       if(N==0)  { //base case 1

           return 0;        }

       else if(N==1){//base case 2

           return 1;        }

       else if(N%2==1)  {//recursive case when N is odd

           return NoOfOnes(N/2)+1;   }

       else{ //recursive case when N is even

           return NoOfOnes(N/2);   }   }    

public static void main(String[] args) { //start of main function

       Scanner scan = new Scanner(System.in); //creates object of Scanner

       System.out.print("Enter n: ");//prompts user to enter value of N

       int n = scan.nextInt(); //reads the value of n from use

 System.out.print("number of 1’s in the binary representation of " +n+" is: "+NoOfOnes(n)); } } //displays the number of 1’s in the binary representation of n

i will explain the working of the function with an example

Lets say N = 6

if(N==0) this statement evaluates to false as N is not 0

else if(N==1 this statement also evaluates to false as N is not 1

else if(N%2==1) this statement also evaluates to false because N is not odd. If we take modulo of 6 by 2 then the remainder is 0 because 6 is completely divisible by 2 so N = 6 is an even number. So the else part is executed:  

       else{

           return NoOfOnes(N/2);}

This statement calls NoOfOnes recursive by passing N/2 to the method. So this becomes:

return NoOfOnes(6/2)

6/2 = 3

NoOfOnes(3)

Now this method is called again with the value of N = 3. Since 3 is an odd number so the recursive case of odd N is executed:

else if(N%2==1)  {

           return NoOfOnes(N/2)+1;   }

NoOfOnes(3/2)+ 1

NoOfOnes(1) + 1

Now the method is called again with value of N=1. The base case 2 is executed because N==1 which returns 1

else if(N==1){

           return 1;   }

So this becomes

NoOfOnes(1) + 1

1 + 1

= 2

So the method return 2 as the number of 1’s in the binary representation of N = 6

Hence the output of the above program with N = 6 is

number of 1’s in the binary representation of 6 is: 2          

You might be interested in
Joaquin is considering a career as an animator and wants to know more about the daily work of an average animator. Which strateg
Advocard [28]
Lacking a response to my question, I'll assume there's no "correct' answer here. 

Joaquin could contact the university's graphic arts department, or if he's interested in the programming side of it, the computer science department, and ask if they have any information on the topic. If he's already majoring to become an animator, then he should talk to one of his professors to see if they know someone in the business that would speak with Joaquin. 

The faster and better way would be to go to reddit, or some site at which animators congregate (most probably found through Google), read the forums, and create if he's not finding a suitable answer.
3 0
4 years ago
5. Are you more honest in your online communication? Explain your response.
Sati [7]
Multiple research has been conducted with regards to this point of view, but the common conclusion is that it depends more on the platform. People tend to be more dishonest when not comfortable and when given the chance live off their fantasy even for short while online. We all do agree that an example is the social media where in order to get attention we only share positive things. You might want to check speaker Simon Sinek and see rich views and in depth understanding of how technology has changed us.
5 0
4 years ago
What does this sign mean?
yKpoI14uk [10]
A. yield to the car on your right
8 0
3 years ago
Read 2 more answers
DEFINE WHAT COPYRIGHT?
Reika [66]
Copyright is when someone is given the right to print, publish or make a film.
5 0
3 years ago
Read 2 more answers
What are the advantage of an e-library​
Semmy [17]

Answer:

Makes it easier to read... summarize cite electronic versions of editions

3 0
1 year ago
Other questions:
  • Most programming languages provide loop statements that help users iteratively process code. In Coral you can write loops that h
    15·1 answer
  • All of the nested folders you created will carry the same permissions as the __________ until you make changes.
    10·1 answer
  • What is the full form of bcc please tell​
    15·2 answers
  • Edhesive coding practice 3.4​
    12·1 answer
  • What does it mean to search an index or the web? (DONT TAKE ANSWERS FROM THE INTERNET!)
    9·2 answers
  • While your hands are on home row, your left hand rests lightly on _____.
    13·2 answers
  • Plz answer this....quickkkkk​
    7·2 answers
  • Which of the following is not a key component of a structure?
    5·1 answer
  • A system engineer enhances the security of a network by adding firewalls to both the external network and the internal company n
    11·1 answer
  • the term elastic in ec2 refers to the fact that you can easily increase or decrease the number of servers you run to support an
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!