w3resource logo
C Programming Exercises

C Exercises: Convert decimal to binary

C Function : Exercise-6 with Solution

Write a program in C to convert decimal number to binary number using the function.

Sample Solution:

C Code:

#include<stdio.h>

long toBin(int);

int main()
{
    long bno;
    int dno;
	printf("\n\n Function : convert decimal to binary :\n");
	printf("-------------------------------------------\n");    
    printf(" Input any decimal number : ");
    scanf("%d",&dno);
    bno = toBin(dno);
    printf("\n The Binary value is : %ld\n\n",bno);

    return 0;
}
long toBin(int dno)
{
    long bno=0,remainder,f=1;
    while(dno != 0)
    {
         remainder = dno % 2;
         bno = bno + remainder * f;
         f = f * 10;
         dno = dno / 2;
    }
    return bno;
}

Sample Output:

 Function : convert decimal to binary :
-------------------------------------------
 Input any decimal number : 65

 The Binary value is : 1000001

C Programming Code Editor:

#include<stdio.h>

long toBin(int);

int main()
{
    long bno;
    int dno;
	printf("\n\n Function : convert decimal to binary :\n");
	printf("-------------------------------------------\n");    
    printf(" Input any decimal number : ");
    scanf("%d",&dno);
    bno = toBin(dno);
    printf("\n The Binary value is : %ld\n\n",bno);

    return 0;
}
long toBin(int dno)
{
    long bno=0,remainder,f=1;
    while(dno != 0)
    {
         remainder = dno % 2;
         bno = bno + remainder * f;
         f = f * 10;
         dno = dno / 2;
    }
    return bno;
}

Improve this sample solution and post your code through Disqus.