C Program to convert a number from decimal to binary

In this C Programming example, we will discuss the method to convert a binary number to its decimal representation and also implement the program to convert a number from decimal to binary.

1. How do you convert a decimal number to a binary number?

There are various direct or indirect approaches to convert a decimal number into a binary number. One such indirect approach is to convert a decimal number into another number system (e.g. octal or hexadecimal) and later convert each digit of the number to a binary number.

However, two direct approaches are practiced to convert a decimal number into a binary number-

  • Performing short division by two with the remainder (for integer part)
  • Performing short multiplication by two with the result (For fractional part)

1.1. How to perform short division by two with the remainder (for integer part)?

  • This method comprises dividing the number to be converted.
  • Let the decimal number N. Now, divide this number by 2.
  • Note down the value of the remainder, which will be either 0 or 1
  • Again divide the remaining decimal number till it became 0 and record each remainder of every step.
  • Then write remainders in reverse order which will be an equivalent binary number of a given decimal number.

1.2. Performing short multiplication by two with the result (For fractional part)

  • Let the decimal fractional part be M then multiply this number by 2.
  • Note down the value of the integer part, which will be either 0 or 1.
  • Again multiply the remaining decimal fractional number till it became 0. 
  • Now, record the result of the integer part at every step.
  • The result will be the equivalent fraction binary number of a given decimal number.
Example: Convert decimal number 126 into binary number
Input:
126

Output:
Step 1: 126/2, Remainder = 0, Quotient = 63
Step 2: 63/2, Remainder = 1, Quotient = 31
Step 3: 31/2, Remainder = 1, Quotient = 15
Step 4: 15/2, Remainder = 1, Quotient = 7
Step 5: 7/2, Remainder = 1, Quotient = 3
Step 6: 3/2, Remainder = 1, Quotient = 1
Step 7: 1/2, Remainder = 1, Quotient = 0
126 in decimal is 1111110 in binary

Helpful topics to understand this program better are-


2. C Program to convert a number from decimal to binary

Let’s discuss the execution(kind of pseudocode) for the program to convert a number from decimal to binary in C.

  1. Initially, the program will prompt the user to enter any decimal number and store the value in int num.
  2. Then convertdecimaltobinary(int num) function is invoked and the values are passed and calculation is made using the following steps-
    1. Take the decimal number as a dividend.
    2. Divide this number by 2.
    3. Store the remainder in an array.
    4. Repeat the above two steps until the number is greater than zero.
    5. Print the array in reverse order (which will be an equivalent binary number of a given decimal number).

Let us now implement the above execution of the program to convert a number from decimal to binary in C.

#include <math.h>
#include <stdio.h>
long convertdecimaltobinary(int num);

long convertdecimaltobinary(int num) {
  long binary = 0;
  int rem, i = 1;
  int step = 1;
  while (num != 0) {
    rem = num % 2;
    printf("Step %d: %d/2, Remainder = %d, Quotient = %d\n", step++, num, rem,
           num / 2);
    num /= 2;
    binary += rem * i;
    i *= 10;
  }
  return binary;
}

int main() {
  int num;
  printf("Enter any decimal number: ");
  scanf("%d", &num);
  printf("%d in decimal is %ld in binary", num, convertdecimaltobinary(num));
  return 0;
}

Note: The size of an integer here is assumed to be 32 bits, so the program divides the number by the base. The long data type is used to input long string from the user we can also use int, float, double datatype by declaring its format specifier as used above.

Output
Enter any decimal number: 125
Step 1: 125/2, Remainder = 1, Quotient = 62
Step 2: 62/2, Remainder = 0, Quotient = 31
Step 3: 31/2, Remainder = 1, Quotient = 15
Step 4: 15/2, Remainder = 1, Quotient = 7
Step 5: 7/2, Remainder = 1, Quotient = 3
Step 6: 3/2, Remainder = 1, Quotient = 1
Step 7: 1/2, Remainder = 1, Quotient = 0
125 in decimal is 1111101 in binary

3. Conclusion

In this C Programming example, we have discussed how to convert a number from decimal to binary in C and discussed every process and steps in detail.


Helpful Links

Please follow C Programming tutorials or the menu in the sidebar for the complete tutorial series.

Also for the example C programs please refer to C Programming Examples.

All examples are hosted on Github.


Recommended Books


An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding

Do not forget to share and Subscribe.

Happy coding!! ?

Recommended -

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Index