Convert Decimal into Binary using Recursion in R

In this example, you will learn to convert decimal number into binary by creating a recursive function, convert_to_binary().

To understand this example, you should have the knowledge of following R programming topics:


Example: Binary to Decimal

# Program to convert decimal number into binary number using recursive function
convert_to_binary <- function(n) {
if(n > 1) {
convert_to_binary(as.integer(n/2))
}
cat(n %% 2)
}

Output

> convert_to_binary(52)
110100

In this program, we convert decimal number entered by the user into binary using a recursive function.

Decimal number is converted into binary by dividing the number successively by 2 and printing the remainder in reverse order.