In this R program to convert a decimal number into binary, Accept the user input which we need to convert to binary, and give the number directly to the function convert_to_binary(), Check if the given decimal number is greater than 1. Conversion into binary is done by dividing the number successively by 2 and the remainder is printed in reverse order which will be the binary of that decimal.
Recursion is a process by which a function will call itself continuously until a condition is met. The function call may be direct or indirect.
STEP 1: Call function convert_to_binary()
STEP 2: Pass the decimal number which needs to be converted to binary as decnum to the function.
STEP 3: inside the function check if the given number is greater than 1, if yes call function convert_to_binary() again with decnum/2 as the argument
STEP 4: divide the number decnum successively by 2 and print the remainder
convert_to_binary <- function(decnum) {
if(decnum > 1) {
convert_to_binary(as.integer(decnum/2))
}
cat(decnum %% 2)
}
convert_to_binary(52)
110100