Decimal to Binary & Binary to Decimal Conversion

Decimal to Binary Conversion: Concept

  • The Decimal numbering system, also known as Denary System is the one where each digit in the given number has values of units, tens, hundreds, etc moving from right to left.
  • If we have a number in fractions or points,
    • Then, each number towards the left of the decimal represents the positive part of the given number which is mathematically represented as 100, 101, 102, 103 and so on.
    • Each number towards the right of the decimal point represents the fractional part of the number which can be represented mathematically as 10-1, 10-2, 10-3  and so on.
Decimal System

How to Convert?

  • To convert a given number in decimal number system into another number system; Binary system in this case we factorize the given number with the base ‘2’.
  • As we divide the number by 2 repeatedly, we record the remainder obtained in the process.
  • As the division process reaches the end, we write the remainder obtained in the sequence from the last step to the first.

Let us understand this working with the help of an example. 1001102.

Let us take a random decimal number 38.

Aim: (38)10 = > Binary Number system.

Step 1:

Step 2:

Step 3:

Step 4:

Implementation of Decimal to Binary Conversion

#include<stdio.h>
#include<stdlib.h>
int main ()
{
int arr[10], num, i;
printf ("Enter the number you want to convert: ");
scanf ("%d", &num);
for (i = 0; num > 0; i++)
{
arr[i] = num % 2;
num = num / 2;
}
printf ("\nDecimal to Binary conversion of the Given Number is: ");
for (i = i - 1; i >= 0; i--)
{
printf ("%d", arr[i]);
}
return 0;
}