Binary to decimal converter in c logic error

I was trying to make a binary to decimal converter in c. But the out put that now comes is a random number. Before I that tried, it used to work for binary which consists of all 1s. eg when I enter 111 it used to give me 7. But when I entered 10 or 011 or anything else it gives me some random number. Now it gives me like it's a memory location. Please help. I tried with a online compiler.

 #include <stdio.h>
 #include<math.h>

 int main() {

int i=1, b[100];
char ch;
int decimal=0;
printf("enter binary\n");
ch=getchar();
while(ch!='\n'){
    ch=ch-'0';
    b[i]=ch;
    i++;
    ch=getchar();
}
i=i-1;
while(i>=0){
    decimal=(b[i]*((int)pow(2,i))) + decimal;
    i--;
}
printf("%d",(int)decimal);
return 0;
}


from Recent Questions - Stack Overflow https://ift.tt/3cSwd8i
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Today Walkin 14th-Sept

Hibernate Search - Elasticsearch with JSON manipulation

Spring Elasticsearch Operations