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 230 views
1 answer 201 views
2 answers 218 views
1 answer 115 views
115 views asked Aug 24, 2021 by avibootz
2 answers 292 views
292 views asked Sep 9, 2014 by avibootz
1 answer 148 views
1 answer 147 views
...