Signup/Sign In
PUBLISHED ON: APRIL 17, 2021

Java Program to Print Binary Equivalent of an Integer using Recursion

In this tutorial, we will learn how to print the binary equivalent of an integer using recursion. A recursive function is a function that calls itself. But before moving further, if you are not familiar with the concept of if statements in java, then do check the article on the topic Conditional Statement in Java.

Input: Enter the number: 7

Output: The equivalent binary number is 111

Program 1: Print Binary Equivalent of an Integer using Recursion

In this program, we will see how to print the binary equivalent of an integer using recursion.

Algorithm:

  1. Start
  2. Create an instance of the Scanner class.
  3. Declare a variable to store the number.
  4. Ask the user to initialize the number.
  5. Divide the number by 2.
  6. Store the remainder when the number is divided by 2.
  7. Repeat the above two steps until the number is greater than zero.
  8. Print the number in reverse order.
  9. Stop.

Let us look at the below example for a better understanding of the above algorithm.

//Java Program to convert an integer to binary
import java.util.Scanner;
public class Main
{
    public static void main(String[] args) 
    {
        int n;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number:");
        n = sc.nextInt();
        Main obj = new Main();
        int m = obj.binary(n);
        System.out.println("The binary equivalent is:"+m);
    }
   public static int binary(int n)
    {
        if (n == 1) 
        {
            return 1;
        }
        return binary(n / 2) * 10 + n % 2;
    }
}


Enter the number: 9
The binary equivalent is:1001

Program 2: Print Binary Equivalent of an Integer using Recursion

In this program, we will see how to print the binary equivalent of an integer using recursion.

Algorithm:

  1. Start
  2. Create an instance of the Scanner class.
  3. Declare a variable to store the number.
  4. Ask the user to initialize the number.
  5. Declare a static method that takes one integer argument as the parameter and returns a string.
  6. Use the StringBuilder class to append the results to each other and convert the output builder object to a string using toString() method.
  7. Divide the particular number by 2 and store the remainder in a variable.
  8. Store the integer value of the number after dividing by 2.
  9. Call the function again and append its result with the remainder
  10. After the base condition (number == 0) is met, it returns the StringBuilder object from the method.
  11. Print the result.
  12. Stop.

Let us look at the below example for a better understanding of the above algorithm.

//Java Program to convert an integer to binary
import java.util.Scanner;
public class Main
{
    public static void main(String[] args) 
    {
        int n;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number:");
        n = sc.nextInt();
        Main obj = new Main();
        String m = obj.BRec(n);
        System.out.println("The binary equivalent is:"+m);
    }
    public static String BRec(int num)
    {
    	StringBuilder sB = new StringBuilder();  
        if(num > 0)
        {
            //get the remainder of the number when divided with 2
            int rem = num % 2;
            //get the whole number after the division
            num /=  2;
            sB.append(BRec(num)).append("").append(String.valueOf(rem));
            return sB.toString();
        }
        else 
        {
           return sB.toString();
        }
     }
   
}


Enter the number: 9
The binary equivalent is:1001



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.