Decimal to Binary

Given a Decimal value the task is to convert it to Binary value.

To convert Decimal value to Binary value we divide the number by 2 and take the remainder as the Binary bit. Then the quotient obtained is again divided by 2 and the remainder is taken as next Binary bit. This process is repeated until the number becomes 0.

Example 1: Consider a Decimal value 57.

Division by 2QuotientRemainder (Binary Bit)
57/2281
28/2140
14/270
7/231
3/211
1/201

Now if we read the Binary bits of the adjacent table from bottom to top we will obtain the Binary equivalent of 57 that is 111001.

C
#include <stdio.h>
#include <math.h>
int main()
{
    int r=0,dec=0,bin=0,count=0;
    printf("Enter a number: ");
    scanf("%d",&dec);
    while(dec>0)
    {
        r=dec%2;
        bin=bin+(r*(int)pow(10,count));
        count++;
        dec=dec/2;
    }
    printf("Binary Value: %d",bin);
    return 0;
}
C