Help me to rectify the problem for binary to decimal conversion program its only giving output when all the digits are same( 1). | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Help me to rectify the problem for binary to decimal conversion program its only giving output when all the digits are same( 1).

https://code.sololearn.com/c0tUXT1mnJJD/?ref=app

27th Mar 2020, 7:50 AM
Sai Dinesh
Sai Dinesh - avatar
4 Answers
0
you wrote if a[num] ==1 it's if a[j] ==1, and I think that you should the max index should be num-1.
27th Mar 2020, 8:15 AM
John Robotane
John Robotane - avatar
0
Thanks but it is not working like u have said..
27th Mar 2020, 8:23 AM
Sai Dinesh
Sai Dinesh - avatar
0
#include <stdio.h> #include<math.h> int main() { int a[100], num, i, j, sum = 0; scanf("%d", &num); for (i = 0; i <= num - 1; i++) { scanf("%d", &a[i]); } for (j = num - 1; j >= 0; j--) { if (a[j] == 1) { sum += pow(2,num-1-j); } } printf("%d", sum); return 0; }
27th Mar 2020, 9:03 AM
John Robotane
John Robotane - avatar
0
Thanks alot bro ❣️❣️❣️
27th Mar 2020, 9:20 AM
Sai Dinesh
Sai Dinesh - avatar