How to convert decimal number to binary number in C

1 Answer

0 votes
#include <stdio.h> 

long DecimalToBinary(long n);

int main(void)
{   
    long decimal;

    printf("Enter a decimal number: ");
    scanf("%ld", &decimal);
    printf("The binary number of %ld is %ld\n", decimal, DecimalToBinary(decimal));
     
    return 0;
}
 
long DecimalToBinary(long n) 
{
    int remainder; 
    long binary = 0, i = 1;
  
    while (n != 0) 
    {
        remainder = n % 2;
        n = n / 2;
        binary = binary + (remainder * i);
        i = i * 10;
    }
    return binary;
}


 
/*
run:
   
Enter a decimal number: 255
The binary number of 255 is 11111111

*/

 



answered Oct 27, 2016 by avibootz

Related questions

1 answer 357 views
1 answer 319 views
2 answers 373 views
1 answer 214 views
214 views asked Aug 24, 2021 by avibootz
2 answers 482 views
482 views asked Sep 9, 2014 by avibootz
1 answer 239 views
1 answer 256 views
...