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 195 views
1 answer 132 views
1 answer 225 views
3 answers 332 views
2 answers 193 views
...