Home  >  Article  >  Java  >  Print the binary representation of an integer in Java using recursion

Print the binary representation of an integer in Java using recursion

WBOY
WBOYforward
2023-09-08 14:41:02950browse

Print the binary representation of an integer in Java using recursion

Recursion is a powerful programming technique that works by breaking a problem into smaller, more tractable sub-problems and applying the same algorithm to solve them. In the world of Java programming, recursion proves to be an invaluable tool for printing binary representations of integers. The binary equivalent, expressed in a base-2 number system with only two digits 0 and 1, presents a common challenge in the field.

In this article, we will set out to shed light on the complexities of printing the binary equivalent of an integer using recursion in Java. Our exploration will include an in-depth examination of syntax, algorithms, and two different methods that can be used to accomplish this task. The initial approach involves using helper methods to concatenate with strings, while the second approach requires the use of a "StringBuilder" for efficient string concatenation. In this article, we provide comprehensive code examples along with output to illustrate the implementation and utilization of these methods.

method

  • Method 1 - Auxiliary method with string concatenation

  • Method 2 − StringBuilder for string concatenation

grammar

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 complexity of printing the binary equivalent of an integer using recursion is as follows -

  • Step 1 - Make a method called "printBinary" that accepts an integer "n" as input.

  • Step 2 - In the "printBinary" method, evaluate whether "n" exceeds 0.

  • Step 3 − If 'n' is greater than 0, use 'n' divided by 2 as input and call the 'printBinary' method recursively.

  • Step 4 - After the recursive call, generate the binary number at the current position by printing the remainder of 'n' divided by 2.

  • Step 5 - Keep repeating steps 3-4 until 'n' reaches 0, which will serve as the base case for recursion.

  • method one

    In this innovative approach, we use a helper method called 'printBinaryHelper', which contains an extra parameter marked 'binary', which is a string. When we call the 'printBinaryHelper' method recursively, we cleverly concatenate the remainder of 'n' divided by 2 with the existing 'binary' string, creating a seamless integration. Once the value of 'n' reaches 0, we proudly print out the final 'binary' string, which elegantly symbolizes the binary representation of the input integer.

    The following is the same program code.

    The Chinese translation of

    Example-1

    is:

    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
    

    Method 2

    In this innovative approach, we use 'StringBuilder' to accurately track complex binary numbers while calling the 'printBinary' method recursively. 'StringBuilder' proves to be an efficient string concatenation tool without the need to create additional string objects, thereby enhancing performance compared to traditional string concatenation methods. After the recursive process successfully completes, the 'StringBuilder' is converted to a string representation, showing the binary equivalent of the input integer, in a fascinating display of technical prowess.

    The following is the same program code.

    The Chinese translation of

    Example-2

    is:

    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
    

    in conclusion

    Recursion is a powerful technique in programming, showing its power in solving a variety of tasks, including printing the binary representation of an integer in Java. In this comprehensive tutorial, we explore two different approaches to achieving optimal recursion using string concatenation and the powerful `StringBuilder`. With a deep understanding of the syntax, algorithms, and skillful implementation of these methods, you can now easily use the power of recursion to print binary representations of integers in Java. As you begin this coding journey, carefully choose an approach that is compatible with your unique needs and consider the performance impact that string concatenation may have in your application. Armed with these insights, you can master the art of recursion in Java programming and unleash the full potential of this powerful technique in your coding efforts.

The above is the detailed content of Print the binary representation of an integer in Java using recursion. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete