C program to convert Decimal to Binary number

In this article, we are going to learn how to convert Decimal to Binary numbers. Here we are going to learn about two number systems one is binary and another is Decimal. Binary numbers are base 2 numbers and represented in 0s and 1s. Decimal numbers are base 10 numbers and are represented in numbers from 0 to 9.

Program to convert decimal to binary number


In this C program we are taking input decimal number from user and convert to binary number system using the logic written in below program.

#include <stdio.h>

int main()
{
    long long decimalNum, tmpDecimalNo;
    long long  binNum;
    int rem, place = 1;

    binNum = 0;
    
    printf("Please enter the decimal number: ");
    scanf("%lld", &decimalNum);

    tmpDecimalNo = decimalNum;

   
    while(tmpDecimalNo > 0)
    {
        rem = tmpDecimalNo % 2;

        binNum = (rem * place) + binNum;

        tmpDecimalNo = tmpDecimalNo/2;

        place *= 10;
    }

    printf("\nThe Binary equivalent of Decimal number %lld is %lld ",decimalNum,binNum);  

    return 0;
}

Output

Please enter the decimal number: 12

The Binary equivalent of Decimal number 12 is 1100

2.C program to convert from Decimal to Binary number


In this example, we are going to write logic to make higher-range conversions. In the above example, we can convert numbers up to 18 binary bits. So for higher range numbers, we can use the below logic.

Note: In this code, we are using the custom function strrevv() to reverse the binary string. But if you are running this code on the windows platform then you can use strrev() function which is available in string.h.strrev() is not available on Linux so we have to write our own custom strrevv().

#include <stdio.h>
#include <string.h>

char *strrevv(char *str)
{
      char *ch1, *ch2;

      if (! str || ! *str)
            return str;

      for (ch1 = str, ch2 = str + strlen(str) - 1; ch2 > ch1; ++ch1, --ch2)
      {
            *ch1 ^= *ch2;
            *ch2 ^= *ch1;
            *ch1 ^= *ch2;
      }
      return str;
}

int main()
{
    long decimalNum, tmpDecimalNo;
    char binNum[65];
    int index = 0;
    
   
    printf("Please enter the decimal number: ");
    scanf("%ld", &decimalNum);
    
    
    tmpDecimalNo = decimalNum;
    
    while(tmpDecimalNo > 0)
    {
        binNum[index] = (tmpDecimalNo % 2) + '0';
        
        tmpDecimalNo =tmpDecimalNo / 2;

        index++;
    }

    binNum[index] = '
#include <stdio.h>
#include <string.h>
char *strrevv(char *str)
{
char *ch1, *ch2;
if (! str || ! *str)
return str;
for (ch1 = str, ch2 = str + strlen(str) - 1; ch2 > ch1; ++ch1, --ch2)
{
*ch1 ^= *ch2;
*ch2 ^= *ch1;
*ch1 ^= *ch2;
}
return str;
}
int main()
{
long decimalNum, tmpDecimalNo;
char binNum[65];
int index = 0;
printf("Please enter the decimal number: ");
scanf("%ld", &decimalNum);
tmpDecimalNo = decimalNum;
while(tmpDecimalNo > 0)
{
binNum[index] = (tmpDecimalNo % 2) + '0';
tmpDecimalNo =tmpDecimalNo / 2;
index++;
}
binNum[index] = '\0';
strrevv(binNum);
printf("\nThe Binary equivalent of Decimal number %ld is %s ",decimalNum,binNum);
return 0;
}
'; strrevv(binNum); printf("\nThe Binary equivalent of Decimal number %ld is %s ",decimalNum,binNum); return 0; }

Output

Please enter the decimal number: 4

The Binary equivalent of Decimal number 4 is 100