Print Binary Equivalent of an Integer using Recursion in Java Last Updated : 18 Sep, 2022 Comments Improve Suggest changes Like Article Like Report Given an integer number as input, we need to write a program to convert the given Integer number into an equivalent binary number by using JAVA. BigInteger class Is used for the mathematical operation which involves very big integer calculations that are outside the limit of all available primitive data types. Examples: Input : 1 Output: 1 Input : 12 Output: 1100 Input : 32 Output: 100000 Algorithm Keep the track of remainder when the number is divided by 2 in an array.Divide the number by 2.Repeat the above two steps until the number is greater than zero.Print the array in reverse order now. Step-wise Execution: Suppose the binary number is 20. Remainder, when 20 is divided by 2, is zero. Therefore, a[0] = 0.Divide 20 by 2. New number is 20/2 = 10.The remainder, when 10 is divided by 2, is zero. Therefore, a[1] = 0.Divide 10 by 2. New number is 10/2 = 5.The remainder, when 5 is divided by 2, is 1. Therefore, a[2] = 1.Divide 5 by 2. New number is 5/2 = 2.The remainder, when 2 is divided by 2, is zero. Therefore, a[3] = 0.Divide 2 by 2. New number is 2/2 = 1.The remainder, when 1 is divided by 2, is 1. Therefore, a[4] = 1.Divide 1 by 2. New number is 1/2 = 0.Since number becomes = 0. Print the array in reverse order. Therefore, the equivalent binary number is 10100. The below image shows it more clearly, the process. Below is the implementation of the above idea in JAVA. Java // Java Program to Print Binary // Equivalent of an Integer // using Recursion import java.util.*; class GFG { public static int binaryConv(int n) { if (n == 1) { return 1; } return binaryConv(n / 2) * 10 + n % 2; } public static void main(String[] args) { int N = 20; System.out.println(binaryConv(N)); } } Output10100 Time Complexity: O (log(n)) Auxiliary Space: O(logn) for call stack B. Conversion using BigInteger class Java // Java Program to Print Binary // Equivalent of an Integer // using Recursion import java.util.*; import java.math.*; class GFG { public static BigInteger binaryConv(BigInteger n) { if (n.compareTo(BigInteger.valueOf(1)) == 0) { return BigInteger.valueOf(1); } return ((binaryConv(n.divide(BigInteger.valueOf(2))).multiply(BigInteger.valueOf(10))).add(n.mod(BigInteger.valueOf(2)))); } public static void main(String[] args) { BigInteger N = new BigInteger("9876543210987543210"); System.out.println(binaryConv(N)); } } Output1000100100010000100001111011100011100011101101010101101010101010 Time Complexity: O (log(n)) Comment More infoAdvertise with us Next Article Print Binary Equivalent of an Integer using Recursion in Java K kushwahp1234 Follow Improve Article Tags : Java Technical Scripter Java Programs Technical Scripter 2020 Practice Tags : Java Similar Reads Java Program to Convert Binary Code Into Equivalent Gray Code Using Recursion Convert the Binary code of the Number into it's equivalent Gray's code using recursion. Binary is the default way to store numbers, but in many applications, binary numbers are not useful and a variation of binary is needed. Gray code has the property that two successive numbers differ in only one b 3 min read Java Program to Find Sum of N Numbers Using Recursion Recursion is a process by which a function calls itself repeatedly till it falls under the base condition and our motive is achieved. To solve any problem using recursion, we should simply follow the below steps: Assume/Identify the smaller problem from the problem which is similar to the bigger/ori 4 min read Java Program to Find Reverse of a Number Using Recursion Recursion is a process by which a function calls itself repeatedly till it falls under the base condition and our motive is achieved. To solve any problem using recursion, we should simply follow the below steps: Step 1: Assume the smaller problem from the problem that is similar to the bigger/origi 5 min read Java Program to Increment by 1 to all the Digits of a given Integer Given an integer, the task is to generate a Java Program to Increment by 1 All the Digits of a given Integer. Examples: Input: 12345 Output: 23456 Input: 110102 Output: 221213 Approach 1: In this approach, we will create a number which will be of the same length as the input and will contain only 1 2 min read Java Program to Compute the Sum of Numbers in a List Using Recursion ArrayList is a part of the Collection framework and is present in java.util package. It provides us with dynamic arrays in Java. Though, it may be slower than standard arrays but can be helpful in programs where lots of manipulation in the array is needed. This class is found in java.util package. I 5 min read Like