Learn about C Program to Convert Decimal to Binary using function in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.
Contents
C Program to Convert Decimal to Binary using function
Source code:
#include<stdio.h>
int convertToBinary(int n);
int main()
{
int num, binary;
printf("Enter Decimal Number: ");
scanf("%d",&num);
binary = convertToBinary(num);
printf("Binary Value= %d\n",binary);
return 0;
}
int convertToBinary(int n)
{
int rem, bin=0, i=1;
while(n!=0)
{
rem= n%2;
bin += (rem*i);
n /= 2;
i *= 10;
}
return bin;
}
Output:
Enter Decimal Number: 20 Binary Value= 10100
Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.
Similar Codes :
C Program to Convert Decimal to Binary
Fahrenheit to Celsius using For loop in C