R Program to convert decimal into binary using recursion

How to convert decimal number into binary by using a recursion

Here we are explaining how to write an R program to convert a decimal number into binary by creating a recursion. Give the required number as an argument to the recursive function. Conversion into binary is done by dividing the number successively by 2 and the remainder is printed in reverse order.

How factors of a number check are implemented in R Program

Given below are the steps which are used in the R program to convert a decimal number into binary. In this R program, we give the number directly to the function convert_to_binary(). Check if the given decimal number is greater than 1 if yes divide the number successively by 2. The remainder is printed in reverse order.

ALGORITHM

STEP 1: Call function convert_to_binary()

STEP 2: Pass the decimal number which needs to be converted to binary as decnum.

STEP 3: check if the given number is greater than 1

STEP 4:if yes divide the number successively by

STEP 5: print the remainder in reverse order

 

R Source Code

                                          convert_to_binary <- function(decnum) {
if(decnum > 1) {
convert_to_binary(as.integer(decnum/2))
}
cat(decnum %% 2)
}
convert_to_binary(52)
                                      

OUTPUT

110100