Skip to content
Home » C Program to Convert Decimal to Binary

C Program to Convert Decimal to Binary

Learn about C Program to Convert Decimal to Binary in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.

C Program to Convert Decimal to Binary

Program :

 #include<stdio.h>
 int main()
 {
     int dec, bin=0, rem, i=1;

     printf("Enter Decimal number: ");
     scanf("%d",&dec);

     while(dec!=0)
     {
         rem = dec % 2;
         bin += (rem*i); // bin = bin + rem*i
         dec /= 2; //dec = dec / 2
         i *= 10; //i=i*10
     }

     printf("Binary= %d\n",bin);

     return 0;
 }

Output:

Enter Decimal number: 24
Binary= 11000

Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.

Similar Codes :
Fahrenheit to Celsius using For loop in C
Fahrenheit to Celsius C Program using While loop