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
What is the value of 25 x 103 = ________?
ira [324]

Answer:

2575

heres coorect

6 0
3 years ago
Read 2 more answers
Please explain in simple words. What does a root do to your tablet? And how do you do it? Is it worth doing it if you only play
nadya68 [22]
Root is a term for android devices similar to "jailbreak" for Apple devices. Rooting gives you basically full control of your device but is considered to be a third party software because you are modifying the operating system. I would watch a YouTube video on how to root your device. I tried to root my device so I could get this screen recording app to work. I think it is really up to you because I think it avoids warranty.
6 0
3 years ago
What a register in cpu does ?​
Rufina [12.5K]

Answer:

CPU register) is one of a small set of data holding places that are part of the computer processor. A register may hold an instruction, a storage address, or any kind of data (such as a bit sequence or individual characters). Some instructions specify registers as part of the instruction.

6 0
3 years ago
Which of these benefits can be achieved whensoftware is restructured?
Ivenika [448]

Answer:

iv. all of the given options

Explanation:

This is the correct answer because this is what happens when software is restructured.

<em>PLEASE</em><em> </em><em>DO MARK</em><em> </em><em>ME AS</em><em> </em><em>BRAINLIEST</em><em> </em><em>IF</em><em> </em><em>MY ANSWER</em><em> </em><em>IS HELPFUL</em><em> </em><em>;</em><em>)</em><em> </em>

4 0
3 years ago
What is the name of a popular high-level computer programming and scripting language that is the name of a snake?
Murljashka [212]
The name of the programming language is PYTHON.
Python is a popular, high level programming language that is used for general purpose programming. Python is objected oriented  and it uses dynamic typing for memory management. The programming language was created in the late 80's by Guido Van Rossum.
7 0
4 years ago
Other questions:
  • Write a program that prints the following 45 pairs of numbers:
    10·1 answer
  • Wired Equivalent Privacy (WEP) is an IEEE 802.11 wireless protocol which provides security algorithms for data confidentiality d
    14·1 answer
  • U2- an example of __________ is an attempt by an unauthorized user to gain access to a system by posing as an authorized user.
    6·1 answer
  • Keyshia now wants to remove the background of the image. She marks the areas she wants to keep and the areas she wants to remove
    13·2 answers
  • The is a blinking vertical line that indicates where the next typed character will appear
    12·1 answer
  • A spreadsheet has some values entered: Cell A1 contains 10, cell A2 contains 14, cell A3 contains 7. You enter in cell A4 the fo
    7·2 answers
  • Which invention spurred the development of the digital camera?
    8·1 answer
  • Create an array using the makeRandomArray method, then take a start time using System.currentTimeMillis(). Next, run the array t
    13·1 answer
  • Guys how can i video my self from my laptop <br> my laptop is (lenovo)
    8·2 answers
  • Which of the following includes premium content you must pay to use?<br> On word
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!