#include <stdio.

h> main() { int n, c, k; printf("Enter an integer in decimal number system\n"); scanf("%d",&n); printf("%d in binary number system is:\n", n); for ( c = 31 ; c >= 0 ; c-- ) { k = n >> c; if ( k & 1 ) printf("1"); else printf("0"); } printf("\n"); return 0; }

C code to store decimal to binary conversion in a string
#include <stdio.h> #include <stdlib.h> char *decimal_to_binary(int); main() { int n, c, k; char *pointer; printf("Enter an integer in decimal number system\n"); scanf("%d",&n); pointer = decimal_to_binary(n); printf("Binary string of %d is: %s\n", n, t); free(pointer); return 0; } char *decimal_to_binary(int n) { int c, d, count; char *pointer; count = 0; pointer = (char*)malloc(32+1); if ( pointer == NULL )

rem. for ( c = 31 . printf(“Its Binary equivalent is = %ld\n”. } *(pointer+count) = '\0'. bin). count++. base = base * 10. if ( d & 1 ) *(pointer+count) = 1 + '0'. no_of_1s = 0. dnum). no_of_1s). dnum. return } pointer.of 1′s in the binary number is = %d\n”. printf(“No. } /* End of main() */ /*————————————————– Output Enter a decimal integer . c-. &num). bin = 0. base = 1.exit(EXIT_FAILURE).of 1s*/ { no_of_1s++. } printf(“Input number is = %d\n”.) { d = n >> c. scanf(“%ld”.h> void main() { long num. #include <stdio. c >= 0 . if ( rem == 1 ) /*To count no. dnum = num. else *(pointer+count) = 0 + '0'. } bin = bin + rem * base. while( num > 0 ) { rem = num % 2. printf(“Enter a decimal integer\n”). num = num / 2 .

&decimal). // Convert a decimal integer do a binary string 2. . char binary[80]. { 11. return 0. int main() 10.75 Input number is = 75 Its Binary equivalent is = 1001011 No.binary). 16. 13. long decimal.binary). 5. 9. 7. // Turbo C modified for Pelles C vegaseat 19nov2004 4. 17.of 1′s in the binary number is = 1 Read more: http://www. // added a test printf() you can remove later 3. getchar(). getchar(). // trap enter 20.com/c-program-to-convert-decimal-to-binarynumber-and-count-number-of-1s-in-it/2586#ixzz1uVhujhR2 1. void dec2bin(long decimal. #include <stdio.h> 6. 18. dec2bin(decimal. printf("\n\n Enter an integer value : "). 15.decimal. 8. 19. // wait 21. 14.of 1′s in the binary number is = 4 RUN2 Enter a decimal integer 128 Input number is = 128 Its Binary equivalent is = 10000000 No.indianshout. printf("\n The binary value of %ld is %s \n". 12. char *binary). scanf("%ld".

int remain. printf("%d/2 = %d remainder = %d\n".22. decimal. } 41. if (neg_flag) 54. do 42. char temp[80]. // accepts a decimal integer and returns a binary coded string 26. int old_decimal. 31. { 29. neg_flag = 1. decimal = -decimal. int k = 0. int neg_flag = 0. 32. 53. // take care of negative input 36. // for test 33. // reverse the spelling 59. // 25. // whittle down the decimal number 46. } 23. 47. // for test 44. 51.sign 55. // add . // this is a test to show the action 48. { 43. while (k >= 0) 60. } while (decimal > 0). n = 0. // 27. remain). 30. binary[n++] = temp[--k]. 45. 40. temp[k++] = ' '. 34. 52. char *binary) 28. // converts digit 0 or 1 to character '0' or '1' 50. 24. old_decimal. remain = decimal % 2. { 38. else 56. 49. void dec2bin(long decimal. if (decimal < 0) 37. temp[k++] = '-'. 35. // space 57. old_decimal = decimal. 39. . decimal = decimal / 2. 58. temp[k++] = remain + '0'.

&n). // end with NULL 63. while(n>0) { b[i]=n%2.b[100].j--) printf("%d". getch(). for(i=j. n=n/2. i++.n. } printf("\n\nBinary Equivalent:").j.h> void main() { int i. clrscr(). } #include<stdio. binary[n-1] = 0.h> #include<conio. } . 62.j>=0. scanf("%d". i=i-1.61. printf("Enter a Number:"). i=0.b[j]).