How to convert decimal number to binary using recursion in Java

1 Answer

0 votes
public class MyClass {
    static int toBinary(int decimal) {
        if (decimal == 0)
            return 0;
        else
            return(decimal % 2 + 10 * toBinary(decimal / 2));
    }
    public static void main(String args[]) {
        int n = 10;

        System.out.println(toBinary(n));
    }
}




/*
run:

1010

*/

 



answered Mar 16, 2021 by avibootz

Related questions

2 answers 216 views
1 answer 145 views
1 answer 245 views
3 answers 357 views
2 answers 202 views
...