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:
- Start
- Create an instance of the Scanner class.
- Declare a variable to store the number.
- Ask the user to initialize the number.
- Divide the number by 2.
- Store the remainder when the number is divided by 2.
- Repeat the above two steps until the number is greater than zero.
- Print the number in reverse order.
- 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:
- Start
- Create an instance of the Scanner class.
- Declare a variable to store the number.
- Ask the user to initialize the number.
- Declare a static method that takes one integer argument as the parameter and returns a string.
- Use the StringBuilder class to append the results to each other and convert the output builder object to a string using toString() method.
- Divide the particular number by 2 and store the remainder in a variable.
- Store the integer value of the number after dividing by 2.
- Call the function again and append its result with the remainder
- After the base condition (number == 0) is met, it returns the StringBuilder object from the method.
- Print the result.
- 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