Monday 21 November 2016

Convert large decimal numbers to their Binary equivalent.

The other day I stumbled upon this post on codechef which deals with calculating factorials of huge numbers (the factorials of which are way beyond the range of int or long int or long long int).

I noticed that the post suggests to use an array to store the result in form of single digits in an array. Thats when I remembered encountering the same problem while making a program to convert decimal numbers to binary numbers as the resulting numbers are way too large for any in built data type.

This program stores all the remainders in an array and a counter variable keeps track of the number of digits in the array.
Then after it is done calculating all the remainders it just prints them all on the screen in reverse order and voila! That does the job. See screen shot down below.

Unfortunately the largest number you can convert at the moment is 2147483647 as it is the largest number in the range of long int. I will try to solve this in my next post though. Till then Happy Coding.

Give it a go in ideone here.




No comments:

Post a Comment