C-language Decimal binary code instance _c language

Source: Internet
Author: User

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);
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.