Print Binary Equivalent of an Integer using Recursion in Java

Recursion, a potent programming technique, entails the resolution of a problem by decomposing it into smaller, more manageable sub problems and applying the same algorithm to solve them. In the realm of Java programming, recursion proves to be an invaluable tool when it comes to printing the binary representation of an integer. The binary equivalent, expressed in a base-2 numeral system that employs only two digits, 0 and 1, poses a common challenge in the field.

In this article, we shall embark to unravel the intricacies of printing the binary equivalent of an integer using recursion in Java. Our exploration will encompass an in-depth examination of the syntax, algorithm, and two distinct approaches that can be employed to accomplish this task. The initial approach involves the utilization of a helper method in tandem with string concatenation, while the second approach entails utilizing a StringBuilder for efficient string concatenation. Throughout this article, we shall furnish comprehensive code examples complete with outputs to vividly illustrate the implementation and utilization of these approaches.

Approaches

• Approach 1 − Helper Method with String Concatenation

• Approach 2 − StringBuilder for String Concatenation

Syntax

public class BinaryPrinter {
public static void printBinary(int n) {
if (n > 0) {
printBinary(n / 2);
System.out.print(n % 2);
}
}

public static void main(String[] args) {
int num = 10; // Example input
System.out.print("Binary equivalent of " + num + " is: ");
printBinary(num);
}
}


Algorithm

The intricacies of printing the binary equivalent of an integer using recursion are as follows −

• Step 1 − Craft a method titled 'printBinary' that accepts an integer 'n' as input.

• Step 2 − Within the 'printBinary' method, assess if 'n' surpasses 0.

• Step 3 − In the event that 'n' exceeds 0, invoke the 'printBinary' method recursively with 'n' divided by 2 as the input.

• Step 4 − Following the recursive call, generate the binary digit at the current position by printing the remainder of 'n' divided by 2.

• Step 5 − Continue to repeat steps 3-4 until 'n' reaches 0, which will serve as the base case for the recursion.

Approach 1

In this innovative approach, we employ a supplementary method called 'printBinaryHelper' that incorporates an additional parameter labeled as 'binary', which is a string of characters. As we invoke the 'printBinaryHelper' method recursively, we skillfully concatenate the remainder of 'n' divided by 2 with the existing 'binary' string, forming a seamless integration. Once the value of 'n' reaches 0, we triumphantly print the conclusive 'binary' string, which elegantly symbolizes the binary representation of the input integer.

Below is the program code for the same.

Example-1

public class BinaryPrinter {
public static void printBinary(int n) {
printBinaryHelper(n, "");
}

public static void printBinaryHelper(int n, String binary) {
if (n > 0) {
printBinaryHelper(n / 2, n % 2 + binary);
} else {
System.out.println("Binary equivalent: " + binary);
}
}

public static void main(String[] args) {
int num = 10; // Example input
System.out.print("Binary equivalent of " + num + " is: ");
printBinary(num);
}
}


Output

Binary equivalent of 10 is: Binary equivalent: 1010


Approach 2

In this innovative approach, we employ a 'StringBuilder' to meticulously keep track of the intricate binary digits while invoking the 'printBinary' method in a recursive manner. The 'StringBuilder' proves to be a highly efficient tool for string concatenation without the need to create additional string objects, thereby augmenting performance in comparison to conventional string concatenation methods. Upon the successful completion of the recursive process, the 'StringBuilder' is converted into a string representation, unveiling the binary equivalent of the input integer in a mesmerizing display of technological prowess.

Below is the program code for the same.

Example-2

public class BinaryPrinter {
public static void printBinary(int n) {
System.out.print("Binary equivalent: ");
StringBuilder binary = new StringBuilder();
printBinaryHelper(n, binary);
System.out.println(binary.toString());
}

public static void printBinaryHelper(int n, StringBuilder binary) {
if (n > 0) {
printBinaryHelper(n / 2, binary);
binary.append(n % 2);
}
}

public static void main(String[] args) {
int num = 10; // Example input
System.out.print("Binary equivalent of " + num + " is: ");
printBinary(num);
}
}


Output

Binary equivalent of 10 is: Binary equivalent: 1010


Conclusion

Recursion, a formidable technique in programming, proves its prowess in solving a myriad of tasks, including the printing of an integer's binary equivalent in Java. In this comprehensive tutorial, we explore two distinct approaches that utilize string concatenation and the powerful StringBuilder for optimal recursion. By gaining a thorough understanding of the syntax, algorithm, and proficient implementation of these approaches, you are now equipped to effortlessly print the binary equivalent of an integer in Java using the power of recursion. As you embark on this coding journey, be sure to meticulously select the approach that aligns harmoniously with your unique requirements, while taking into consideration the potential performance implications of string concatenation in your application. With these insights, you are well-prepared to master the art of recursion in Java programming, and unlock the full potential of this powerful technique in your coding endeavors.

Updated on: 07-Nov-2023

57 Views