## Problem

We are required to write a program to find the binary representation of a number.## Designing Algorithm

To design the logic to do the job, let's first recall how we convert decimal numbers to binary on paper. Let's say, we have a number 27. We will write it on paper:Now continue repeating the procedure until you reach zero:

After completing the procedure, you can write the binary by gathering all the remainders from bottom to top.

So the binary of 27 will be 11011.

## Iterative Solution

We will do the same thing in our program. We will create an empty string to store the binary number. Then we will continue dividing the number by 2 and add the remainder to the left of the binary string in each step. We will continue this procedure until we reach zero.### Code

public String toBinary(int number){

//Define empty String

String binary="";

//Continue until the number is greater than zero

while(number>0){

//Find the remainder and append it to the binary String

int rem=number%2;

binary=rem+binary;

//Find the quotient and leave it for next iteration

number/=2;

}

return binary;

}

//Define empty String

String binary="";

//Continue until the number is greater than zero

while(number>0){

//Find the remainder and append it to the binary String

int rem=number%2;

binary=rem+binary;

//Find the quotient and leave it for next iteration

number/=2;

}

return binary;

}

## Recursive Solution

Recursion can also be used to solve the problem. For example, to find the binary of 27, we can perform only one step:The remainder for 27 is 1. We can call the function again to find the binary of 13. The binary of 13 is 1101 so we can attach the remainder of 27 which is 1 at the end of 1101 to get 11011 which is the binary of 27. In recursion, our base case will be that the binary of 0 is 0.

### Code

public String toBinary(int number){

//Base case. Binary of 0 is 0

if (number==0)

return "0";

//Find the remainder

int rem=number%2;

//Attach the remainder to the end of the binary of quotient

String binary=toBinary(number/2)+rem;

return binary;

}

//Base case. Binary of 0 is 0

if (number==0)

return "0";

//Find the remainder

int rem=number%2;

//Attach the remainder to the end of the binary of quotient

String binary=toBinary(number/2)+rem;

return binary;

}

## Full Code

package decimaltobinary;

/**

*

* @author umersoftwares

*/

import java.util.Scanner;

public class DecimalToBinary {

public static void main(String[] args) {

Scanner in=new Scanner(System.in);

int inp=in.nextInt();

String binaryIter=toBinaryIteration(inp);

String binaryRec=toBinaryRecursion(inp);

System.out.println("Binary by Iteration = "+binaryIter);

System.out.println("Binary by Recursion = "+binaryRec);

}

public static String toBinaryIteration(int number){

//Define empty String

String binary="";

//Continue until the number is greater than zero

while(number>0){

//Find the remainder and append it to the binary String

int rem=number%2;

binary=rem+binary;

//Find the quotient and leave it for next iteration

number/=2;

}

return binary;

}

public static String toBinaryRecursion(int number){

//Base case. Binary of 0 is 0

if (number==0)

return "0";

//Find the remainder

int rem=number%2;

//Attach the remainder to the end of the binary of quotient

String binary=toBinaryRecursion(number/2)+rem;

return binary;

}

}

/**

*

* @author umersoftwares

*/

import java.util.Scanner;

public class DecimalToBinary {

public static void main(String[] args) {

Scanner in=new Scanner(System.in);

int inp=in.nextInt();

String binaryIter=toBinaryIteration(inp);

String binaryRec=toBinaryRecursion(inp);

System.out.println("Binary by Iteration = "+binaryIter);

System.out.println("Binary by Recursion = "+binaryRec);

}

public static String toBinaryIteration(int number){

//Define empty String

String binary="";

//Continue until the number is greater than zero

while(number>0){

//Find the remainder and append it to the binary String

int rem=number%2;

binary=rem+binary;

//Find the quotient and leave it for next iteration

number/=2;

}

return binary;

}

public static String toBinaryRecursion(int number){

//Base case. Binary of 0 is 0

if (number==0)

return "0";

//Find the remainder

int rem=number%2;

//Attach the remainder to the end of the binary of quotient

String binary=toBinaryRecursion(number/2)+rem;

return binary;

}

}

## Conclusion

Two approaches were discussed to find the binary of a number. The iterative approach is more efficient than the recursive approach because, in recursion, the binary of the number in each instance of the function is being stored separately whereas in the iterative approach, the same variable binary is used. If you have any confusion about any of them, feel free to ask in the comments.Stay updated. Download our app from Google Play Store.

## 0 Comments

Thanks for leaving a comment. I will try to reply you soon.