-7

I don't know what is wrong, please advice.

Write a recursive function int ones(int x), that returns the number of ones in the binary representation of x. Make your function work independent of the size of an int (16, 32, 64 bits).

import java.util.Scanner;

public class Recursion1 {

    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter the desired binary number: ");
        int input = sc.nextInt();
        int ones(int x) {
            int count = 0;
            while(x!=0) {
                if(x==1)
                    count++;
                System.out.println("Number of ones is : "+ count);
            }
        }
    }
}
10
  • See this question, is quite similar: stackoverflow.com/questions/9199984/basic-java-recursion-method Commented Feb 27, 2018 at 17:31
  • @Andreas: Well...there's one... :-) Commented Feb 27, 2018 at 17:31
  • 2
    Note how your ones function is embedded inside main. Java does not allow that. Commented Feb 27, 2018 at 17:31
  • 1
    @T.J.Crowder Ok, ok, you're right, but main doesn't really count. Would be interesting to see a recursive main though. Commented Feb 27, 2018 at 17:32
  • @Andreas: :-) I did it once (adjusting the args), then said "Don't be silly Teej" and made it its own function. Commented Feb 27, 2018 at 17:32

4 Answers 4

0

Other people pointed out your mistake and gave some ideas. I just wanted to give another solution with bit operation.

public class MyClass {

    public static int ones(int x) {
        if (x <= 1) return x;

        return x % 2 + ones(x >> 1);
    }

    public static void main(String args[]) {
        int x = 63;

        System.out.println("Ones in " + x + " : " + ones(x));
    }
}

Prints out 6

Sign up to request clarification or add additional context in comments.

Comments

0

You have an incongruity in your problem statement, your function int ones(int x) doesn't work with "independent of the size of an int (16, 32, 64 bits)". A java int is 32 bits, so to handle ints bigger than that, you'll need to use either String or BigInteger. I have provided a case below for BigInteger. Also in your problem statement, you are to accept an integer, not a binary representation of an integer, and your function is to take this integer and not the binary representation.

import java.math.BigInteger;
import java.util.Scanner;

public class BinaryCounter {
    private static BigInteger one = new BigInteger("1");
    private static BigInteger two = new BigInteger("2");

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter the desired INTEGER: ");
        BigInteger input = sc.nextBigInteger();

        System.out.println("Number of ones is: " + ones(input));
    }

    // x has to be a String or BigInteger to handle numbers larger than 32 bits
    static int ones(BigInteger x) {
        // eventually when we get to the end, we have to stop the recursion
        if (x.compareTo(one) == 0) { return 1; }

        int remainder = x.mod(two).intValue();
        BigInteger half = x.divide(two);

        return remainder + ones(half);
    }
}

Comments

0

The ones method must be outside main. Then it must call itself (a recursive function, by definition, calls itself).

It receives a String (a text), then it checks if the first character is 1. Then it sums this value to ones(rest of the string), where "rest of the string" is all the characters but the first. In the end, you count all of them, recursivelly.

public static void main(String args[]){
    Scanner sc = new Scanner(System.in);
    System.out.println("Please enter the desired binary number: ");
    String input = sc.next();

    System.out.println("Number of ones is: " + ones(input));
}

static int ones(String x) {
    // check if first character is 1 or zero
    char c = x.charAt(0);
    int count = c == '1' ? 1 : 0;

    // if it's the only character, return the resulst
    if (x.length() == 1) {
        return count;
    }

    // if there are more characters, call ones(rest of the string)
    return count + ones(x.substring(1));
}

Comments

-1

For recursion you need to call the same function itself until it reaches a certain condition. A popular example of recursion is as below:

public static long factorial(int n) { 
    if (n == 1) {
        return 1; 
    }
    return n * factorial(n-1); 
}

Please note here how the function factorial( int n) calls itself until n decreases to 1.

2 Comments

What does this answer have to do with OP's question, apart from being a common example of recursion?
She's just confused on what recursion is so provided her a simple example. Should I not be doing such?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.