0% found this document useful (0 votes)
225 views

Decimal To Binary and Vice Versa Conversion

1. The documents provide C code to convert decimal numbers to binary representations. 2. One program stores the binary conversion in a string. Another counts the number of 1's in the binary number. 3. The code uses loops and modulo operations to iterate through each binary digit of the decimal number input by the user.

Uploaded by

DK
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
225 views

Decimal To Binary and Vice Versa Conversion

1. The documents provide C code to convert decimal numbers to binary representations. 2. One program stores the binary conversion in a string. Another counts the number of 1's in the binary number. 3. The code uses loops and modulo operations to iterate through each binary digit of the decimal number input by the user.

Uploaded by

DK
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

#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 )

exit(EXIT_FAILURE); for ( c = 31 ; c >= 0 ; c-- ) { d = n >> c; if ( d & 1 ) *(pointer+count) = 1 + '0'; else *(pointer+count) = 0 + '0'; count++; } *(pointer+count) = '\0'; return } pointer;

#include <stdio.h> void main() { long num, dnum, rem, base = 1, bin = 0, no_of_1s = 0; printf(Enter a decimal integer\n); scanf(%ld, &num); dnum = num; while( num > 0 ) { rem = num % 2; if ( rem == 1 ) /*To count no.of 1s*/ { no_of_1s++; } bin = bin + rem * base; num = num / 2 ; base = base * 10; } printf(Input number is = %d\n, dnum); printf(Its Binary equivalent is = %ld\n, bin); printf(No.of 1s in the binary number is = %d\n, no_of_1s); } /* End of main() */

/* Output Enter a decimal integer

75 Input number is = 75 Its Binary equivalent is = 1001011 No.of 1s in the binary number is = 4 RUN2 Enter a decimal integer 128 Input number is = 128 Its Binary equivalent is = 10000000 No.of 1s in the binary number is = 1

Read more: http://www.indianshout.com/c-program-to-convert-decimal-to-binarynumber-and-count-number-of-1s-in-it/2586#ixzz1uVhujhR2

1. // Convert a decimal integer do a binary string 2. // added a test printf() you can remove later 3. // Turbo C modified for Pelles C vegaseat 19nov2004 4. 5. #include <stdio.h> 6. 7. void dec2bin(long decimal, char *binary); 8. 9. int main() 10. { 11. long decimal; 12. char binary[80]; 13. 14. printf("\n\n Enter an integer value : "); 15. scanf("%ld",&decimal); 16. dec2bin(decimal,binary); 17. printf("\n The binary value of %ld is %s \n",decimal,binary); 18. 19. getchar(); // trap enter 20. getchar(); // wait 21. return 0;

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

61. 62. binary[n-1] = 0; // end with NULL 63. }

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

You might also like