Convert Decimal into Binary using Recursion in R


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.

Your builder path starts here. Builders don't just know how to code, they create solutions that matter.

Escape tutorial hell and ship real projects.

Try Programiz PRO
  • Real-World Projects
  • On-Demand Learning
  • AI Mentor
  • Builder Community