C Program to Convert Decimal Number to Binary Number

June 5, 2021

This program converts a decimal number to an equivalent binary number.

Example: Program to convert Decimal to Binary

In this program, we have created a user defined function decimalToBinary() for decimal to binary conversion. The programs takes decimal number (entered by user) as input and converts it into a binary number using the function decimalToBinary(). To understand this program, you should have the basic idea of following C programming topics:

  1. C – Functions
  2. C – while loop
#include <stdio.h>
#include <math.h>

long decimalToBinary(int decimalnum)
{
    long binarynum = 0;
    int rem, temp = 1;

    while (decimalnum!=0)
    {
        rem = decimalnum%2;
        decimalnum = decimalnum / 2;
        binarynum = binarynum + rem*temp;
        temp = temp * 10;
    }
    return binarynum;
}

int main()
{
    int decimalnum;
    printf("Enter a Decimal Number: ");
    scanf("%d", &decimalnum);
    printf("Equivalent Binary Number is: %ld", decimalToBinary(decimalnum));
    return 0;
}

Output:

Enter a Decimal Number: 234
Equivalent Binary Number is: 11101010

admin