Convert Decimal To Binary C Program

By | December 17, 2015

C Program To Convert Decimal To Binary Number

Learn How To Convert Decimal To Binary in C Programming Language. It is important that we should know How A For Loop Works before getting further with the C Program Code.

A Decimal Number consists of values from 0 to 9 and a Binary Number consists of only 0 and 1. To change a Decimal Integer into Binary value, a Modulus Operator in C has to be used.

Example

Binary Equivalent of 10 is 1010.

gcc test.c -lm

Method 1: C Program For Decimal To Binary Conversion using If – Else

#include<stdio.h>
#include<stdlib.h>

void conversion(int num, int base)
{
      int remainder = num % base;	
      if(num == 0)
      {
            return;
      }
      conversion(num / base, base);
      if(remainder < 10)
      {
            printf("%d", remainder);	
      }
}

int main()
{
      int num, choice;
      printf("\nEnter a Positive Decimal Number:\t");
      scanf("%d", &num);
      printf("\nBinary Value:\t");
      conversion(num, 2);
      printf("\n");
      return 0;
}

Method 2: C Program To Convert Decimal To Binary Value without Array

#include<stdio.h>

int main()
{
      int dec_num, rem, a = 1, temp;
      long int bin_num = 0;
      printf("\nEnter A Decimal Integer:\t");
      scanf("%d", &dec_num);
      temp = dec_num;
      while(dec_num > 0)
      {
            rem = dec_num%2;
            dec_num = dec_num/2;
            bin_num = bin_num + (a * rem);
            a = a * 10;
      }
      printf("\nBinary Equivalent of Decimal Integer %d: %ld", temp, bin_num);
      printf("\n");
      return 0;
}

Method 3: Convert Decimal Number To Binary in C using Functions

#include<stdio.h>

int decimal_to_binary(int dec_num)
{
      int rem, a = 1;
      long int bin_num = 0;
      while(dec_num > 0)
      {
            rem = dec_num%2;
            dec_num = dec_num/2;
            bin_num = bin_num + (a * rem);
            a = a * 10;
      }
      return bin_num;lang:default decode:true
}

int main()
{
      int dec_num, temp;
      printf("\nEnter A Decimal Integer:\t");
      scanf("%d", &dec_num);
      temp = dec_num;
      printf("\nBinary Equivalent of Decimal Integer %d: %d", temp, decimal_to_binary(dec_num));
      printf("\n");
      return 0;
}

Output

C Program To Convert Decimal To Binary Value

If you have any compilation errors or doubts in this C Program To Convert Decimal to Binary Number, let us know about in the Comment Section below. Find more about Decimal Number System on Britannica.

Recommended Programs
C Program For Hexadecimal To Binary Number Conversion
C Program For Hexadecimal To Decimal Number Conversion
C Program For Decimal To Octal Number Conversion
C Program For Octal To Decimal Number Conversion
C Program For Decimal To Hexadecimal Number Conversion
C Program For Binary To Decimal Number Conversion
C Program To Convert Lowercase Characters into Uppercase
C Program For Converting Decimal into Binary, Hexadecimal and Octal Values
C Program To Find Special Numbers

3 thoughts on “Convert Decimal To Binary C Program

  1. Pooja Chauhan

    Thanks for so many methods for converting a Decimal number into Binary values.

    Reply
  2. Vikas Dixit

    Wow! Decimal To Binary Conversion using If Else is so simple. Thanks. I finally understood the logic of conversion.

    Reply
  3. Mehak Arora

    The first code for Decimal To Binary Conversion using Recursion is too good. It is just short simple and sweet.

    Reply

Let's Discuss