C Program to Convert Decimal to Binary

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

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

Then, refer to the step-by-step process of converting decimal to binary. Now let's move on to the program.

Decimal to binary in C

To convert a decimal number to a binary number in C programming, you have to ask the user to enter the number (in the decimal number system) to convert it into a binary number and then display the equivalent value in binary as an 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 in the Code::Blocks IDE, here is the sample run after a successful build and 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 the ENTER key to see its equivalent binary value as shown in the second snapshot of the sample run given here:

decimal to binary in c

Program Explained

Decimal to Binary in C without using an array

The question is: write a program in C that converts a decimal number to binary without using an 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 the previous program.

Decimal to Binary in C without a Modulus Operator

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

The question is, "Write a program in C that converts decimal to binary without using the modulus 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 a 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 in the bin[] array as shown in the program given below. The question is, "Write a program in C that converts a decimal number to a binary number using a 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 the start of the program), the array (bin[]) and variable (i) both stay known and can be used throughout the program. The value of i is declared as a static variable. Because a static variable holds its previous value. And one more thing about static variables: if you do not initialize any value to a static variable initially, then it holds 0 as its initial value.

The same program in different languages

C Quiz


« Previous Program Next Program »


Liked this post? Share it!