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
It is a life long skills like spelling, it is a skill of typing data currently and quickly using a computer keyboard
viva [34]
Is this a riddle bro?
6 0
3 years ago
Advanced Electronics has employees who use a company website to share work. By posting on the website, employees are
Anna11 [10]

Answer:

B - On the Save & Send tab, select Save to SharePoint, which will allow access to all registered users.

Explanation:

When you send the document to SharePoint, within the SharePoint app you can enable <em>track changes </em>which can be used to track your changes.

7 0
3 years ago
Srooy but can you guys help me with this​(sorry again miss click
telo118 [61]

what miss click let me know when you got the real one      

5 0
3 years ago
James, a system administrator, is tasked to update the BIOS and firmware of the computer in his organization. What are the reaso
Xelga [282]

Answer:

One reason to update BIOS and firmware is to ensure they support new hardware like CPU model or graphic card. In some cases, the updates can also resolve overheating issue or motherboard issue.  

However, this is not always the case that we must update the BIOS every time there is a new release because the latest update may cause problem to existing system as well.

As a general rule, only when there are some problems with the existing BIOS and firmware, we are only required to update them.

7 0
3 years ago
Define a python functions that draw a pinwheel using the pinwheel function that has input paramiters that allows you to change t
QveST [7]

Start with your draw_pinwheel() function

During each iteration of the loop decide which color to set the turtle to using the color() function

On even iterations, use color1

On odd iterations, use color2

Use an if/else statement to do the decision making

After deciding the color, surround a call to draw_triangle() with begin_fill() and end_fill() so that drawing the triangle creates a colored triangle.

If you have have forgotten, you can use an if/else to check for even/oddness in python as follows.

my_number = 3

if(my_number % 2 == 0):     # the remainder of dividing by 2 is 0

   print("The number is "+str(my_number))

   print("The number is EVEN")

else:                       # the remainder must be 1

   print("The number is "+str(my_number))

   print("The number is ODD")

Good luck <3

8 0
3 years ago
Other questions:
  • ____ are not associated with data from the database and are used to display such things as the report's title
    15·1 answer
  • What is the definition of the components that motherboards, ssds, processors, and cases are part of?
    10·1 answer
  • why is this message poor or ineffective ,Due to low profits,there will be no annual year-end bonus this year. We hope to have a
    13·2 answers
  • Select the correct answer.
    7·1 answer
  • Design a ROM of size 64X8 bit in VHDL. Your ROM takes as input an address and a clock and output the content of the ROM at the c
    7·1 answer
  • Python
    5·1 answer
  • What are the Components of a CPU?
    15·2 answers
  • What are the text or graphics that are lighter than the main text located in a document's background called?
    10·1 answer
  • How to can you avoid becoming a victim of identity theft?
    10·1 answer
  • NO LINKS Please
    8·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!