added BinaryEquivalent file in Recursive

This commit is contained in:
Chiranjeev
2021-08-19 14:50:01 +05:30
parent 9434b413e7
commit 1d381c5032

View File

@ -0,0 +1,26 @@
/*
* Problem Statement: Given a positive number `num`, find it's binary equivalent using recursion
*
* What is Binary Equivalent?
* - In binary number system, a number is represented in terms of 0s and 1s,
* for example:
* - Binary Of 2 = 10
* - Binary of 3 = 11
* - Binary of 4 = 100
*
* Reference on how to find Binary Equivalent
* - https://byjus.com/maths/decimal-to-binary/
*
*/
const binaryEquivalent = (num) => {
if (num === 0 || num === 1) {
return String(num)
}
return binaryEquivalent(Math.floor(num / 2)) + String(num % 2)
}
// Driver Code
const num = 6
const ans = binaryEquivalent(num)
console.log(ans)