/* * To convert a decimal number into its binary representation * @author-Karan Narain(karan@iitk.ac.in) * */ #include void dectobin(int dec, int bin[], int *count) { int k = 0, n = 0; int remain; int old_decimal; // for displaying int temp[80]; if (dec < 0) dec = -dec; if(dec==0) { bin[0]=0; *count=1; return; } while (dec > 0) { old_decimal = dec; // for displaying remain = dec % 2; // quotient becomes the new decimal number dec = dec / 2; printf("%d/2 = %d remainder = %d\n", old_decimal, dec, remain); // store the remainders as they are computed in a temporary array temp temp[k++] = remain; } // reverse the temp array to get binary representation in correct order while (k >= 0) bin[n++] = temp[--k]; *count=n-1; } int main() { long dec; int bin[80],i; int count; printf("\n\n Enter an integer value : "); scanf("%ld",&dec); dectobin(dec,bin,&count); printf("\n The binary value of %ld is ",dec); for(i=0;i