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);
}
}
}
}
onesfunction is embedded insidemain. Java does not allow that.maindoesn't really count. Would be interesting to see a recursivemainthough.