Convert Binary to Decimal and from Decimal to Binary
Expand Post »
Hi, I have a program problem with converting decimals to binary and back again. My teacher said that the program is to be like a menu and convert decimals or binary from the user's input. This is part of my code:
I know my problem is with the binary(int number) function because it is runnable but dosen't call to the function. So, my question is how can I modify this to run correctly? Also, how would I go about reversing the code to have it convert binary to decimal?
Last edited by Narue; Nov 20th, 2008 at 6:51 pm. Reason: added code tags
Re: Convert Binary to Decimal and from Decimal to Binary
Thanks Rohitman. So, How do I reverse it as in binary to decimal? I know it has something to do with diffrent powers of a number like 2x^i. So, do I use a for loop for that or something else?
Thanks Rohitman. So, How do I reverse it as in binary to decimal? I know it has something to do with diffrent powers of a number like 2x^i. So, do I use a for loop for that or something else?
yes, you have to do some thing like that only. It your wish to use the recursive function or not but I don't recommend to use the recursive function in this case and earlier one too.
In any number system is represented by the general expression
a[n]*r^[n] + a[n-1]*r^[n-1] +........+ a[2]*r^[2] + a[1]*r^[1]+a[0] //integer part
+ a[-1]*r^[-1] + + a[-1]*r^[-2].......+ a[-m]*r^[-m] //fraction part
where,
a[j] is the coefficient
r is radix or base
For decimal no system r = 10; and coefficient can have the value from 0-9
Similarly the binary no system base will be 2, and can have only 2 values 0 and 1.
I guess concept would be clear...
best of luck for programming..
Last edited by Rhohitman; Nov 20th, 2008 at 9:50 pm.
Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.
This thread is more than three months old
No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.