- 1
Please give me a error solution in this program.
#include<stdio.h> #include<string.h> #include<math.h> int main() { char getbtd[]="1101.1010"; int length,dc=0,i=0,p1=0,p2=-1; float sum=0; length=strlen(getbtd); while(1) { if(getbtd[i]=='.') break; else dc++; i++; } for(i=dc-1;i>=0;i++) { sum = sum+((int)(getbtd[i]-'0')*pow(2,p1)); p1++; } for(i=dc+1;i<length;i++) { sum=sum+((int)(getbtd[i]-'0')*pow(2,p2)); p2--; } printf("Decimal No is:%f",sum); return 0; }
6 Respuestas
+ 3
First of all the semicolon after
return 0;
Then second your for loop do not end.
+ 2
Replace <studio.h> with <stdio.h>
What is the code supposed to do? can you describe that?
(Edit)
Snippet modified.
+ 2
This loop should be:
for(i=dc-1;i>=0;i--)
{
sum = sum+((int)(getbtd[i]-'0')*pow(2,p1));
p1++;
}
and the solution can be seen as follows:
https://code.sololearn.com/cgIKDXzntAl9/?ref=app
+ 2
Missing- semicolon after return 0;
Loop do not end.
0
Replace <studio.h> with <stdio.h>
What is the code supposed to do? can you describe that?
To convert binary to decimal
0
Yash,
Do you have to do this manually? cause if not, you can use `strtol` function from <stdlib.h>
https://en.cppreference.com/w/cpp/string/byte/strtol