Ruby program to convert the decimal number to binary using recursion

I hope every article can give you some new information. In that sense today we are going to know very clearly about Ruby program to convert the decimal number to binary using recursion. Let’s go into our article.

Ruby program to convert the decimal number to binary using recursion

Ruby program to convert the decimal number to binary using recursion

# Ruby program to convert the decimal number 
# to binary using recursion

def dec2bin(num)
    if num == 0
        return 0;
    else
        return num % 2 + 10 * dec2bin(num / 2);
    end
end

print "Enter number: ";
number = gets.chomp.to_i;  

result = dec2bin(number);

print "Binary equivalent is: ",result;
Enter number: 8
Binary equivalent is: 1000

Final Words

Ruby program to convert the decimal number to binary using recursion Article We hope you have all the information you need. Let me know if you have any doubts about this article. Thanks.

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Deekshi. Here, I post about programming to help developers.

Share on:

Leave a Comment