Binary to decimal horners method | Sololearn: Learn to code for FREE!

+1

Binary to decimal horners method

I want to write program in C which converts binary number to decimal. I have the function Int convert(int *T, int n) { w=T[] ; For(int i=0;i<n;i++) w=w*2+T[i] Return w; } But i dont know if its ok or not, can someone explain please? I dont know how to use it

4/21/2021 3:52:47 PM

Klaudiusz Biegacz

4 Answers

New Answer

+2

Well it is really helpful and it works thank you for that, but still with pow method it wont be done with the horners one. I need it to use the horners scheme.

+1

I am assuming the integer array T contains the digits of the binary number as elements and n is the size of the array. Then you would need to use the special math header file and use the pow() function for calculating the return value, w. The following are the hints: - use '#include <math.h>' - Initialize the return value, w to 0. - in the for loop, use 'w=w+pow(2,i);' The convert function should be called in the main function of C for it to be executed. Request you to also check the syntax in C Please tell me if you need the full code as well

+1

Hi Klaudiusz, understood. The following code for the convert function will work : int convert (int* T, int n) { int w=0, twos=1; int i; for (i=0; i<n; i±±) { w+=(T[i] * twos) ; twos =twos * 2; } return w; }

+1

Thanks for Your help! It works