#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
int main(){
	char input[10];
   int i,j,digit,out[10],flag=0,sum;
   while(flag==0){
   	sum=0;
   	flag=0;
      strcpy(input,"          ");
   	scanf("%s",&input);
   	fflush(stdin);
   	digit=strlen(input);
   	printf("%d\n",flag);
   	if(strlen(input)>8){break;}
   	/*else {
   		for(i=0;i<8;i++){
      		     if (input[i]!=48&&input[i]!=49){
            	       flag++;
         	       break;
         	     }
      	        }
   	}*/
   	printf("%d\n",digit);
   	printf("%d",flag);
   	if (flag==1){break;}
   	for(i=0;i<8;i++){
   		if(input[i]!='\0'){
      		if(input[i]=='1'){
         		out[i]=1;}
         	else if(input[i]=='0'){
         		out[i]=0;}
      	}
   	}
      j=1;
   	for(i=0;i<digit;i++){
   		sum+=out[i]*pow(2,digit-j);
         j++;
      }
      printf("Decimal = %d",sum);
   }
}

Program closed right after I input any character, if I un-comment codes I commented up there. The code I commented supposed to be filter for non binary number so if non binary number is inputted, the the program terminates.
Anyway I got hint about adding code 'atoi' to ease it up. Still i got no idea how to separate each number afterward.

Please help :)

I don't know what your problem is, but I appreciate your efforts.
Divide your decimal number by 2. Record the remainder(should be 0 or 1) divide again and record again until your decimal number becomes zero. The record of 0 and 1 digits is your binary number.
Do the reverse for decimal to binary.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
int main(){
	char input[10];
   int i,j,digit,out[10],flag=0,sum;
   while(flag==0){
   	sum=0;
   	flag=0;
      strcpy(input,"          ");
   	scanf("%s",&input);
   	fflush(stdin);
   	digit=strlen(input);
   	printf("%d\n",flag);
   	if(strlen(input)>8){break;}
   	/*else {
   		for(i=0;i<8;i++){
      		     if (input[i]!=48&&input[i]!=49){
            	       flag++;
         	       break;
         	     }
      	        }
   	}*/
   	printf("%d\n",digit);
   	printf("%d",flag);
   	if (flag==1){break;}
   	for(i=0;i<8;i++){
   		if(input[i]!='\0'){
      		if(input[i]=='1'){
         		out[i]=1;}
         	else if(input[i]=='0'){
         		out[i]=0;}
      	}
   	}
      j=1;
   	for(i=0;i<digit;i++){
   		sum+=out[i]*pow(2,digit-j);
         j++;
      }
      printf("Decimal = %d",sum);
   }
}

Program closed right after I input any character, if I un-comment codes I commented up there. The code I commented supposed to be filter for non binary number so if non binary number is inputted, the the program terminates.
Anyway I got hint about adding code 'atoi' to ease it up. Still i got no idea how to separate each number afterward.

Please help :)

i think when u write this

for(i=0;i<8;i++)

the code will only work if you enter exactly 8 digits...otherwise it will cause an error..so try writing it like this

for(i=0;i<strlen(input);i++)

hope this helps...cheers

I don't know what your problem is, but I appreciate your efforts.
Divide your decimal number by 2. Record the remainder(should be 0 or 1) divide again and record again until your decimal number becomes zero. The record of 0 and 1 digits is your binary number.
Do the reverse for decimal to binary.

I don't understand how that converts binary to decimal.

What's the significance of this piece of code?

for(i=0;i<8;i++){
      		     if (input[i]!=48&&input[i]!=49){
            	       flag++;
         	       break;
         	     }

Your problem is pretty simple and you've already solved it yourself too. The main part is realising this:

j=1;
   	for(i=0;i<digit;i++){
   		sum+=out[i]*pow(2,digit-j);
         j++;

Flow of the program:

1. Take the input.
2. Convert characters to digits.
3. Store the digits in an array. (Last 2 steps can be done in a single step.)
4. Apply the code above.
5. print the sum. I dunno what everything else is doing in the program.

Also on a related note, don't use fflush(stdin)

Do the reverse for decimal to binary.

I don't understand how that converts binary to decimal.

Well I shall do the reverse:
Take 101
Instead of divide, multiply by 2 for every "remainder":
1 x 2^0 = 1
0 x 2^1 = 0
1 x 2^2 = 4

1+0+4=5 decimal which is 101 binary.

I don't know what your problem is, but I appreciate your efforts.
Divide your decimal number by 2. Record the remainder(should be 0 or 1) divide again and record again until your decimal number becomes zero. The record of 0 and 1 digits is your binary number.
Do the reverse for decimal to binary.

Well I shall do the reverse:
Take 101
Instead of divide, multiply by 2 for every "remainder":
1 x 2^0 = 1
0 x 2^1 = 0
1 x 2^2 = 4

1+0+4=5 decimal which is 101 binary.

Well, that i knew. I guess i just misinterpreted your previous post. :)

Have you learnt bitwise shifting operators? It would be loads easier that way...

strcpy(input,"          ");  // You don't need to do this if your next 
                                   //    line overwrites the value
   	scanf("%s",&input);        // see this
   	fflush(stdin);             // see this
Comments
nice links

i think when u write this

for(i=0;i<8;i++)

the code will only work if you enter exactly 8 digits...otherwise it will cause an error..so try writing it like this

for(i=0;i<strlen(input);i++)

hope this helps...cheers

yes this was the problem, thanks :)
thanks to everyone for replying:twisted:

This question has already been answered. Start a new discussion instead.