C Program to Convert Decimal to Binary

In this tutorial, we will learn about how to create a program in C that converts any given number (in decimal number) by user at run-time into its equivalent value in binary number. At last we have also created a program that uses user-defined function to do the same job.

But before going through the program, if you are not aware of

  • Decimal Number
  • Binary Number
  • Decimal to Binary Conversion

then refer to Decimal to Binary conversion step by step process. Now let's move on to program.

Decimal to Binary in C

To convert decimal number to binary number in C programming, you have to ask from user to enter the number (in decimal number system) to convert it into binary number and then display the equivalent value in binary as output.

#include<stdio.h>
#include<conio.h>
int main()
{
    int decnum, binnum[50], i=0;
    printf("Enter any decimal number: ");
    scanf("%d", &decnum);
    while(decnum!=0)
    {
        binnum[i] = decnum%2;
        i++;
        decnum = decnum/2;
    }
    printf("\nEquivalent Binary Value = ");
    for(i=(i-1); i>=0; i--)
        printf("%d", binnum[i]);
    getch();
    return 0;
}

As the above program was written under Code::Blocks IDE, therefore after successful build and run, here is the sample run. This is the first snapshot of the sample run:

c program convert decimal to binary

Now supply any number (in decimal) say 117 and press ENTER key to see its equivalent binary value as shown in the second snapshot of sample run given here:

decimal to binary in c

Program Explained

  • Receive any decimal number
  • Create a while loop that runs until the value of decimal number (decnum) becomes 0
  • Let's suppose that user has entered 117 as input
  • Therefore at first run of the while loop, decnum!=0 or 117!=0 evaluates to be true, therefore the program flow goes inside the loop
  • And decnum%2 or 117%2 or 1 (as if we divide 117 by 2, then we will get 1 as remainder) gets initialized to binnum[i] (as i gets initialized with 0 at start of the program, therefore at first run of the loop i holds its value as 0) or binnum[0]
  • Now the value of variable i gets incremented and becomes 1, and then decnum/2 or 117/2 or 58 gets initialized to decnum
  • Again program flow goes back at condition of the while loop
  • Therefore at second run of the while loop, decnum!=0 or 58!=0 evaluates to be true, and again program flow goes inside the loop
  • And decnum%2 or 58%2 or 0 (this time while dividing the number 58 by 2, it does not leaves any remainder, therefore remainder will be 0) gets initialized to binnum[1]
  • The value of i gets incremented and becomes 2. And finally decnum/2 or 58/2 or 29 gets initialized to decnum
  • Now at third run of the while loop, decnum!=0 or 29!=0 evaluates to be true, and now the third time program flow goes inside the loop and do the similar steps as told and done in above steps until and unless the value of decnum becomes 0. Here the value of decnum at first, second, third, fourth, fifth, sixth, seventh and eigth run will be 117, 58, 29, 14, 7, 3, 1, and then 0
  • After storing equivalent binary value of given decimal number. We have to print the value of binary digit in reverse order.
  • That is, create a for loop that runs from i-1. Here we have subtracted the value of i with 1, because at last run of the while loop, the value of i gets incremented and decnum/2 gets initialized to decnum. And when the value of decnum becomes 0 (last run of the loop), therefore program flow does not goes inside the loop again after checking the condition decnum!=0 or 0!=0 (that evaluates to be false), but we have already incremented the value of i. Therefore we have to subtract it with 1 to delete its extra index
  • Therefore here, in for loop, we have started the loop from i-1 and runs until it is greater than or equal to 0
  • Print the value of binnum[i] one by one
  • In this way, we have equivalent binary value of given decimal number gets printed on output screen

Decimal to Binary in C without using Array

The question is, write a program in C that converts decimal number to binary without using array. The answer to this question is given below:

#include<stdio.h>
#include<conio.h>
int main()
{
    int decnum, binnum=0, mul=1, rem;
    printf("Enter any decimal number: ");
    scanf("%d", &decnum);
    while(decnum>0)
    {
        rem = decnum%2;
        binnum = binnum+(rem*mul);
        mul = mul*10;
        decnum = decnum/2;
    }
    printf("\nEquivalent Binary Value = %d", binnum);
    getch();
    return 0;
}

This program produces the same output as of previous program.

Decimal to Binary in C without Modulous Operator

If you want to create the same program but without using modulous operator (%), then

  • divide the number by 2 and initialize the quotient value to any variable say temp
  • now multiply the quotient with 2 and again initialize its multiplication result to another variable say chk
  • and then check whether chk is equal to the original value (the current value of decnum, the dividend) or not
  • if it is equal, then no any remainder left, otherwise if it is not equal, then remainder left
  • and if remainder does not left, then we have to initialize 0 to binnum[i] (i holds 0 at first run)
  • otherwise if remainder left, therefore we have to initialize 1 to the initial index (0th index) of binnum array, and again increment the index of array and continue as shown in the program given below

The question is, Write a program in C that converts decimal to binary without using modulous operator. The answer to this question is:

#include<stdio.h>
#include<conio.h>
int main()
{
    int decnum, binnum[50], i=0, temp, chk;
    printf("Enter any Decimal number: ");
    scanf("%d", &decnum);
    while(decnum!=0)
    {
        temp = decnum/2;
        chk = temp*2;
        if(chk==decnum)
            binnum[i] = 0;
        else
            binnum[i] = 1;
        i++;
        decnum = temp;
    }
    printf("\nEquivalent Binary Value = ");
    for(i=(i-1); i>=0; i--)
        printf("%d", binnum[i]);
    getch();
    return 0;
}

Here is the final snapshot of the sample run:

c decimal to binary without modulous

Decimal to Binary in C using User-Defined Function

Now let's create a user-defined function named DecToBin() that receives one argument (the decimal number) to convert and stores its equivalent binary value one by one to bin[] array as shown in the program given below. The question is, Write a program in C that converts Decimal number to Binary number using User-Defined Function. The answer to this question is:

#include<stdio.h>
#include<conio.h>
void DecToBin(int dec);
int bin[50];
static int i;
int main()
{
    int decnum;
    printf("Enter any decimal number: ");
    scanf("%d", &decnum);
    DecToBin(decnum);
    printf("\nEquivalent Binary Value = ");
    for(i=(i-1); i>=0; i--)
        printf("%d", bin[i]);
    getch();
    return 0;
}
void DecToBin(int dec)
{
    while(dec!=0)
    {
        bin[i] = dec%2;
        i++;
        dec = dec/2;
    }
}

Here is the final snapshot of the sample run:

c decimal to binary using function

Here we have declared the array bin[] and variable i outside the function main(), as after declaring both outside the main() function (at start of the program), the array (bin[]) and variable (i) both stays known and can be used throughout the program. The value of i is declared as static variable. Because static variable holds its previous value. And one more thing about static variable is that, if you will not initialize any value to a static variable initially, then it holds 0 as its initial value.

Same Program in Other Languages

C Online Test


« Previous Program Next Program »

Follow/Like Us on Facebook




Subscribe Us on YouTube