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 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 Convert Integer Values into Binary Given an integer in Java, your task is to write a Java program to convert this given integer into a binary number. Example: Input: = 45 Output: = 101101 Input: = 32 Output: = 100000 Integers: Integers are numbers whose base value is 10. The Integer or int data type is a 32-bit signed twoâs complemen 4 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 How to Convert a String Class to an Integer Class in Java? String in Java is used to store the sequence of characters in Java. A string can contain a character, a word, a sentence, or a paragraph. Integer is a Wrapper class that is used to store Integer values in Java. The range of the Integer class is from -2,147,483,648 to 2,147,483,647 (-2^31 to 2^31 - 1 3 min read Java Program to Increment All Element of an Array by One Given the array, the task is to increment each element of the array by 1. Complete traversal is required for incrementing all the elements of an array. An optimized way to complete a given task is having time complexity as O(N). Examples: Input : arr1[] = {50, 25, 32, 12, 6, 10, 100, 150} Output: ar 4 min read Java Program to Find Factorial of a Number Recursively Factorial of a number n is defined as a product of all positive descending integers, Factorial of n is denoted by n!. Factorial can be calculated using the following recursive formula where the recursive call is made to a multiplicity of all the numbers lesser than the number for which the factorial 4 min read Java Program to Convert a Decimal Number to Binary Number using Stacks Java is high level, compiled as well as interpreted programming language. Stack is an abstract data type used in most of the programming languages and can be implemented using arrays or linked list. Stack data structure follows the principle of LIFO (Last In First Out) . Stack allows push, pop, peek 3 min read Like