Using the C language to convert decimal to binary, and statistical conversion of the binary code in the number 1.
#include <stdio.h>
int binarynum[16]; The binary code int count=0 is stored after conversion
; Count decimal integers by 2 to divide the number of
int onecount=0; The number of 1 in the resulting binary is
void main () {
int num;
printf ("Enter a decimal integer:");
scanf ("%d", &num);
while ((NUM/2)!= 1) { //Judgment Condition: divided by 2 after the quotient is not equal to 1
binarynum[count] = num%2; After the remainder of the number deposited in the array
num/= 2; num = NUM/2; Conduct the next round of Judgment
count++; This variable is used to specify the array subscript
}
binarynum[count+1] = 1; After the last division is done, the remaining quotient must be 1, so add a 1 printf at the end
("Binary value is:");
Reverse print the Elements
//sizeof (shape array) in the array/sizeof (shaping a single element size) = The number of elements for
(int i=sizeof (binarynum)/sizeof (int)-1; i> = 0; i--) {
if (binarynum[i] = = 1)
onecount++; Occurs once 1 on the cumulative
printf ("%d", binarynum[i])
;
printf ("\ n Total%d 1\n", onecount);
}