C Program to convert decimal to binary also count the one's In It

For a better understanding, we always recommend you to learn the basic topics of C programming listed below:

What are the decimal and binary number systems?

In this c program, we need to convert the decimal number into binary, and also we need to count the ones. Changing over a decimal number into a binary utilizing the c program is exceptionally straightforward. A binary number is a number that has only zeros and ones. We have to change over these zeros and ones to a number spoken to the decimal framework. The binary number framework is critical after the rise of pcs, which can just read binary numbers called machine language. Binary numbers are written by subscript 2, and decimals are written by subscript 10.

How to convert a decimal to binary in C?

First, we have to declare some variables "num, dnum, rem, basebin, count" as data type long. We use the variable 'num' to store the decimal number, and the 'count' variable to count the number of ones in the binary number. After accepting the decimal number from the user, start a while loop until the number is zero. Use the mod operator to get the remainder. If the remainder is one increment, the variable 'count' by 1. Then apply the formula.

            bin = bin + rem * base
            num = num / 2 
            base = base * 10

 

After that, we will display the input number as 'dnum' and its equivalent Binary number as 'bin' using the printf method. And also, we will Display the number of ones in the Binary number as count.

ALGORITHM

STEP 1: Include the Header files to use the built-in functions in the C program.

STEP 2: Declare the variables 'num, dnum, rem, base, bin, count' as Long Data type.

STEP 3: Read the Decimal Integer number from the user and save it into 'num'.

STEP 4: Copy the number 'num' into 'dnum.'

STEP 5: Using a 'while loop' with the condition 'num>0' do step 6.

STEP 6: Calculate 'rem = num % 2'.Then check if 'rem == 1', if its true then increment Count by 1.This is for getting the number of ones in the Binary number.

STEP 7: Calculate 'bin = bin + rem * base'. and 'num = num/2' and 'base = base * 10' for finding the Binary number

STEP 8: Repeat step 5.

STEP 9: Display input number is 'dnum,' and its equivalent Binary number is a bin.

STEP 10: Display the number of 1's in the Binary number as count.

C Source Code

                                          #include <stdio.h>

void main() {
  long num, dnum, rem, base = 1, bin = 0, count = 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*/ {
      count++;
    }
    bin = bin + rem * base; /*converting decimal into binary*/
    num = num / 2;
    base = base * 10;
  }
  printf("Input number is                   = %d\n", dnum);
  printf("Its Binary equivalent is          = %ld\n", bin);
  printf("No.of 1's in the binary number is = %d\n", count);
} /* End of main() */
                                      

OUTPUT

Enter a decimal integer

75
Input number is                   = 75
Its Binary equivalent is          = 1001011
No.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.of 1's in the binary number is = 1