I have finally found a code that converts decimal to binary. But however I hardly understand it and I think it would be better if I know how to do it for myself instead of using pre fabricated code all the time eh?
I could adapt this code into mine if I understood it completely but I don't so I am goin to post it and hope for sum help. Thanks in advance.
I have commented the main places I don't understand.Code:#include <iostream.h> #include <stdlib.h> int a,b; int n=0; int arr[100]; void main() { cout <<"Decimal : "; cin >>a; while (a>0)//just to be sure a is a number { n++;//add one to your array varibale I would have guessed you did this last. b=a/2; //divide the decimal by 2 and assign it to b if (a%2==0)//if that divsion = 0 { arr[n]=0; //assign 0 to your array variable } else { arr[n]=a-(2*b); //if it doesn't = 0 subtract the awnser of 2 times b } // from the initial decimal not sure why??????????????????? a=b;//unsure why this is here } cout<<"Binary : ";// this section out puts the binary backwards???????????????? for (n=n;n>0;n--) { cout <<arr[n]; } system("PAUSE"); }