Computer Science, asked by itsmecrazy, 11 months ago

write a program to display the binary equivalent of a decimal number .Use recursive function to find out the binary value​

Answers

Answered by dipesh8051
4

Answer:

Examples :

Input : 7

Output :111

Input :10

Output :1010

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

We have discussed one iterative solution in below post.

Program for Decimal to Binary Conversion

____________________________

Below is Recursive solution

findBinary(decimal)

if (decimal == 0)

binary = 0

else

binary = decimal % 2 + 10 * (findBinary(decimal / 2)

.

Step by step process for better understanding of how the algorithm works

Let decimal number be 10.

Step 1-> 10 % 2 which is equal-too 0 + 10 * ( 10/2 ) % 2

Step 2-> 5 % 2 which is equal-too 1 + 10 * ( 5 / 2) % 2

Step 3-> 2 % 2 which is equal-too 0 + 10 * ( 2 / 2 ) % 2

Step 4-> 1 % 2 which is equal-too 1 + 10 * ( 1 / 2 ) % 2

C

// C/C++ program for decimal to binary

// conversion using recursion

#include <stdio.h>

// Decimal to binary conversion

// using recursion

int find(int decimal_number)

{

if (decimal_number == 0)

return 0;

else

return (decimal_number % 2 + 10 *

find(decimal_number / 2));

}

// Driver code

int main()

{

int decimal_number = 10;

printf("%d", find(decimal_number));

return 0;

}

Similar questions