Convert decimal to binary number - C Program

Write a 'C' program to convert decimal number to binary number. (using function).

Solution:

#include <stdio.h>
/* Function to convert a decinal number to binary number */
long DtoB(long n)
{
     int r;
     long binary = 0, i = 1;
     while(n != 0)
     {
          r = n%2;
          n = n/2;
          binary= binary + (r*i);
          i = i*10;
     }
     return binary;
}
int main()
{
     long d,z;
     printf("Enter Decimal Number : ");
     scanf("%ld", &d);
     z=DtoB(d);
     printf("\nBinary Number of %ld  : %ld", d,z);
     return 0;
}


Output:

decimal to binary