JavaScript Program to Convert Decimal to Binary Using Recursion Last Updated : 26 Mar, 2024 Comments Improve Suggest changes Like Article Like Report JavaScript allows us to convert decimal numbers to their binary equivalents using recursion, offering two distinct approaches. Through recursive division and concatenation, the first method constructs the binary representation directly. The second method leverages a string and recursion, providing an alternative approach. Each method's time and space complexities are also discussed, providing insights into their efficiency and suitability for various scenarios. Table of Content Using RecursionUsing Recursion and vector of boolUsing RecursionIn this method, we recursively divide the decimal number by 2 and append the remainder to construct the binary representation. Syntax:findBinary(decimal) if (decimal == 0) binary = 0 else binary = decimal % 2 + 10 * (findBinary(decimal / 2))Example: The below code converts Decimal to Binary Using Recursion JavaScript function findBinary(decimal) { if (decimal == 0) { return 0; } else { return ((decimal % 2) + 10 * findBinary(parseInt(decimal / 2))); } } // Driver code let decimal_number = 10; console.log(findBinary(decimal_number)); Output1010 Time Complexity: O(log2n), Here n is the decimal_number.Auxiliary Space: O(1), As constant extra space is used. Using Recursion and vector of boolIn this approach we recursively divide the decimal number by 2 and instead of directly appending the remainder to construct the binary representation we append the remainder to a string representing the binary digits. Example: The below code converts Decimal to Binary Using Recursion and vector of bool JavaScript let bin_num = ""; function deci_to_bin(x) { if (x <= 1) { bin_num += String.fromCharCode(x + '0'.charCodeAt()); } else { deci_to_bin(parseInt(x / 2, 10)); // If x is divisible by 2 if (x % 2 != 0) bin_num += '1'; // otherwise else bin_num += '0'; } } deci_to_bin(231576); console.log(bin_num); Output111000100010011000 Time Complexity: O(log n), where n is given decimal numberAuxiliary Space: O(log n) Comment More infoAdvertise with us Next Article JavaScript Program to Convert Decimal to Binary Using Recursion A am8254s3a Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Program Similar Reads JavaScript Program to Convert Decimal to Binary In this article, we are going to learn the conversion of numeric values from decimal to binary. Binary is a number system with 2 digits (0 and 1) representing all numeric values. Given a number N which is in decimal representation. our task is to convert the decimal representation of the number to i 5 min read JavaScript Program to Display Fibonacci Sequence Using Recursion In this article, we will explore how to display the Fibonacci sequence using recursion in JavaScript. The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones. Recursion is a powerful technique in programming that involves a function calling itself. A recu 3 min read JavaScript Program to Print N to 1 using Recursion In this article, we will see how to print N to 1 using Recursion in JavaScript. What is Recursion? The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. In the recursive program, the solution to the base 1 min read JavaScript Program to Convert a Number to Binary We are going to learn the conversion of a number to binary by using JavaScript, Convert a number to binary in JavaScript refers to the process of converting a decimal number into its binary representation, which uses only the digits 0 and 1 to represent the value Converting a number to binary in Jav 3 min read JavaScript Program for Decimal to any base conversion In this JavaScript article, we will see how we can do decimal to any base conversion in JavaScript. The base can not be less than 2 and can not exceed 36, So we always have to find out the base of a decimal that lies in between this range, which is '2=< base <=36'. Example: Input: number = "11 5 min read JavaScript Program for Binary Representation of Next Number Given a binary input that represents a binary representation of the positive number n, find a binary representation of n+1. We will try to find out this with the different approaches discussed below. Examples: Input : 10011 Output : 10100 Here n = (19)10 = (10011)2 next greater integer = (20)10 = (1 3 min read JavaScript Program to Find iâth Index Character in a Binary String Obtained After n Iterations Given a decimal number m, convert it into a binary string and apply n iterations. In each iteration, 0 becomes â01â and 1 becomes â10â. Find the (based on indexing) index character in the string after the nth iteration.Input: m = 5, n = 2, i = 3Output: 1Input: m = 3, n = 3, i = 6Output: 1Approach 1C 5 min read Java Program to Convert a Decimal Number to Binary Number using Stacks Java is high level, compiled as well as interpreted programming language. Stack is an abstract data type used in most of the programming languages and can be implemented using arrays or linked list. Stack data structure follows the principle of LIFO (Last In First Out) . Stack allows push, pop, peek 3 min read Java Program to Convert Binary to Hexadecimal The Hexadecimal number system as the name suggests comprises 16 entities. These 16 entities consist of 10 digits, 0-9 representing the first 10 numbers of the hexadecimal system as well. For the remaining 6 numbers, we use English alphabets ranging from A through F to represent the numbers 10 to 15. 6 min read How to convert binary to decimal in JavaScript? A binary number is a number expressed in the binary numeral system, which uses only two symbols 0 and 1. Decimal numbers are base 10 in a JavaScript number system that uses 10 digits from 0 to 9. We are given a number as Binary input we have to convert it into decimal in JavaScript and print the res 3 min read Like