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.

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges